4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
22 * Fallback functions for PHP installed without mbstring support
26 public static function iconv( $from, $to, $string ) {
27 if ( substr( $to, -8 ) == '//IGNORE' ) {
28 $to = substr( $to, 0, strlen( $to ) - 8 );
30 if( strcasecmp( $from, $to ) == 0 ) {
33 if( strcasecmp( $from, 'utf-8' ) == 0 ) {
34 return utf8_decode( $string );
36 if( strcasecmp( $to, 'utf-8' ) == 0 ) {
37 return utf8_encode( $string );
43 * Fallback implementation for mb_substr, hardcoded to UTF-8.
44 * Attempts to be at least _moderately_ efficient; best optimized
45 * for relatively small offset and count values -- about 5x slower
46 * than native mb_string in my testing.
48 * Larger offsets are still fairly efficient for Latin text, but
49 * can be up to 100x slower than native if the text is heavily
50 * multibyte and we have to slog through a few hundred kb.
52 public static function mb_substr( $str, $start, $count='end' ) {
54 $split = self
::mb_substr_split_unicode( $str, intval( $start ) );
55 $str = substr( $str, $split );
58 if( $count !== 'end' ) {
59 $split = self
::mb_substr_split_unicode( $str, intval( $count ) );
60 $str = substr( $str, 0, $split );
66 public static function mb_substr_split_unicode( $str, $splitPos ) {
67 if( $splitPos == 0 ) {
71 $byteLen = strlen( $str );
74 if( $splitPos > 256 ) {
75 // Optimize large string offsets by skipping ahead N bytes.
76 // This will cut out most of our slow time on Latin-based text,
77 // and 1/2 to 1/3 on East European and Asian scripts.
79 while ( $bytePos < $byteLen && $str{$bytePos} >= "\x80" && $str{$bytePos} < "\xc0" ) {
82 $charPos = mb_strlen( substr( $str, 0, $bytePos ) );
88 while( $charPos++
< $splitPos ) {
90 // Move past any tail bytes
91 while ( $bytePos < $byteLen && $str{$bytePos} >= "\x80" && $str{$bytePos} < "\xc0" ) {
96 $splitPosX = $splitPos +
1;
97 $charPos = 0; // relative to end of string; we don't care about the actual char position here
99 while( $bytePos > 0 && $charPos-- >= $splitPosX ) {
101 // Move past any tail bytes
102 while ( $bytePos > 0 && $str{$bytePos} >= "\x80" && $str{$bytePos} < "\xc0" ) {
112 * Fallback implementation of mb_strlen, hardcoded to UTF-8.
114 * @param string $enc optional encoding; ignored
117 public static function mb_strlen( $str, $enc = '' ) {
118 $counts = count_chars( $str );
122 for( $i = 0; $i < 0x80; $i++
) {
123 $total +
= $counts[$i];
126 // Count multibyte sequence heads
127 for( $i = 0xc0; $i < 0xff; $i++
) {
128 $total +
= $counts[$i];
135 * Fallback implementation of mb_strpos, hardcoded to UTF-8.
136 * @param $haystack String
137 * @param $needle String
138 * @param $offset String: optional start position
139 * @param $encoding String: optional encoding; ignored
142 public static function mb_strpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
143 $needle = preg_quote( $needle, '/' );
146 preg_match( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE
, $offset );
148 if( isset( $ar[0][1] ) ) {
156 * Fallback implementation of mb_strrpos, hardcoded to UTF-8.
157 * @param $haystack String
158 * @param $needle String
159 * @param $offset String: optional start position
160 * @param $encoding String: optional encoding; ignored
163 public static function mb_strrpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
164 $needle = preg_quote( $needle, '/' );
167 preg_match_all( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE
, $offset );
169 if( isset( $ar[0] ) && count( $ar[0] ) > 0 &&
170 isset( $ar[0][count( $ar[0] ) - 1][1] ) ) {
171 return $ar[0][count( $ar[0] ) - 1][1];
178 * Fallback implementation of stream_resolve_include_path()
179 * Native stream_resolve_include_path is available for PHP 5 >= 5.3.2
180 * @param $filename String
183 public static function stream_resolve_include_path( $filename ) {
184 $pathArray = explode( PATH_SEPARATOR
, get_include_path() );
185 foreach ( $pathArray as $path ) {
186 $fullFilename = $path . DIRECTORY_SEPARATOR
. $filename;
187 if ( file_exists( $fullFilename ) ) {
188 return $fullFilename;