3 * Fallback functions for PHP installed without mbstring support.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * Fallback functions for PHP installed without mbstring support
34 public static function iconv( $from, $to, $string ) {
35 if ( substr( $to, -8 ) == '//IGNORE' ) {
36 $to = substr( $to, 0, strlen( $to ) - 8 );
38 if ( strcasecmp( $from, $to ) == 0 ) {
41 if ( strcasecmp( $from, 'utf-8' ) == 0 ) {
42 return utf8_decode( $string );
44 if ( strcasecmp( $to, 'utf-8' ) == 0 ) {
45 return utf8_encode( $string );
51 * Fallback implementation for mb_substr, hardcoded to UTF-8.
52 * Attempts to be at least _moderately_ efficient; best optimized
53 * for relatively small offset and count values -- about 5x slower
54 * than native mb_string in my testing.
56 * Larger offsets are still fairly efficient for Latin text, but
57 * can be up to 100x slower than native if the text is heavily
58 * multibyte and we have to slog through a few hundred kb.
62 * @param $count string
66 public static function mb_substr( $str, $start, $count = 'end' ) {
68 $split = self
::mb_substr_split_unicode( $str, intval( $start ) );
69 $str = substr( $str, $split );
72 if ( $count !== 'end' ) {
73 $split = self
::mb_substr_split_unicode( $str, intval( $count ) );
74 $str = substr( $str, 0, $split );
85 public static function mb_substr_split_unicode( $str, $splitPos ) {
86 if ( $splitPos == 0 ) {
90 $byteLen = strlen( $str );
92 if ( $splitPos > 0 ) {
93 if ( $splitPos > 256 ) {
94 // Optimize large string offsets by skipping ahead N bytes.
95 // This will cut out most of our slow time on Latin-based text,
96 // and 1/2 to 1/3 on East European and Asian scripts.
98 while ( $bytePos < $byteLen && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
101 $charPos = mb_strlen( substr( $str, 0, $bytePos ) );
107 while ( $charPos++
< $splitPos ) {
109 // Move past any tail bytes
110 while ( $bytePos < $byteLen && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
115 $splitPosX = $splitPos +
1;
116 $charPos = 0; // relative to end of string; we don't care about the actual char position here
118 while ( $bytePos > 0 && $charPos-- >= $splitPosX ) {
120 // Move past any tail bytes
121 while ( $bytePos > 0 && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
131 * Fallback implementation of mb_strlen, hardcoded to UTF-8.
133 * @param string $enc optional encoding; ignored
136 public static function mb_strlen( $str, $enc = '' ) {
137 $counts = count_chars( $str );
141 for ( $i = 0; $i < 0x80; $i++
) {
142 $total +
= $counts[$i];
145 // Count multibyte sequence heads
146 for ( $i = 0xc0; $i < 0xff; $i++
) {
147 $total +
= $counts[$i];
153 * Fallback implementation of mb_strpos, hardcoded to UTF-8.
154 * @param $haystack String
155 * @param $needle String
156 * @param string $offset optional start position
157 * @param string $encoding optional encoding; ignored
160 public static function mb_strpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
161 $needle = preg_quote( $needle, '/' );
164 preg_match( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE
, $offset );
166 if ( isset( $ar[0][1] ) ) {
174 * Fallback implementation of mb_strrpos, hardcoded to UTF-8.
175 * @param $haystack String
176 * @param $needle String
177 * @param string $offset optional start position
178 * @param string $encoding optional encoding; ignored
181 public static function mb_strrpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
182 $needle = preg_quote( $needle, '/' );
185 preg_match_all( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE
, $offset );
187 if ( isset( $ar[0] ) && count( $ar[0] ) > 0 &&
188 isset( $ar[0][count( $ar[0] ) - 1][1] ) ) {
189 return $ar[0][count( $ar[0] ) - 1][1];