MediaWiki  REL1_21
Fallback.php
Go to the documentation of this file.
00001 <?php
00026 class Fallback {
00027 
00034         public static function iconv( $from, $to, $string ) {
00035                 if ( substr( $to, -8 ) == '//IGNORE' ) {
00036                         $to = substr( $to, 0, strlen( $to ) - 8 );
00037                 }
00038                 if( strcasecmp( $from, $to ) == 0 ) {
00039                         return $string;
00040                 }
00041                 if( strcasecmp( $from, 'utf-8' ) == 0 ) {
00042                         return utf8_decode( $string );
00043                 }
00044                 if( strcasecmp( $to, 'utf-8' ) == 0 ) {
00045                         return utf8_encode( $string );
00046                 }
00047                 return $string;
00048         }
00049 
00066         public static function mb_substr( $str, $start, $count = 'end' ) {
00067                 if( $start != 0 ) {
00068                         $split = self::mb_substr_split_unicode( $str, intval( $start ) );
00069                         $str = substr( $str, $split );
00070                 }
00071 
00072                 if( $count !== 'end' ) {
00073                         $split = self::mb_substr_split_unicode( $str, intval( $count ) );
00074                         $str = substr( $str, 0, $split );
00075                 }
00076 
00077                 return $str;
00078         }
00079 
00085         public static function mb_substr_split_unicode( $str, $splitPos ) {
00086                 if( $splitPos == 0 ) {
00087                         return 0;
00088                 }
00089 
00090                 $byteLen = strlen( $str );
00091 
00092                 if( $splitPos > 0 ) {
00093                         if( $splitPos > 256 ) {
00094                                 // Optimize large string offsets by skipping ahead N bytes.
00095                                 // This will cut out most of our slow time on Latin-based text,
00096                                 // and 1/2 to 1/3 on East European and Asian scripts.
00097                                 $bytePos = $splitPos;
00098                                 while ( $bytePos < $byteLen && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
00099                                         ++$bytePos;
00100                                 }
00101                                 $charPos = mb_strlen( substr( $str, 0, $bytePos ) );
00102                         } else {
00103                                 $charPos = 0;
00104                                 $bytePos = 0;
00105                         }
00106 
00107                         while( $charPos++ < $splitPos ) {
00108                                 ++$bytePos;
00109                                 // Move past any tail bytes
00110                                 while ( $bytePos < $byteLen && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
00111                                         ++$bytePos;
00112                                 }
00113                         }
00114                 } else {
00115                         $splitPosX = $splitPos + 1;
00116                         $charPos = 0; // relative to end of string; we don't care about the actual char position here
00117                         $bytePos = $byteLen;
00118                         while( $bytePos > 0 && $charPos-- >= $splitPosX ) {
00119                                 --$bytePos;
00120                                 // Move past any tail bytes
00121                                 while ( $bytePos > 0 && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
00122                                         --$bytePos;
00123                                 }
00124                         }
00125                 }
00126 
00127                 return $bytePos;
00128         }
00129 
00136         public static function mb_strlen( $str, $enc = '' ) {
00137                 $counts = count_chars( $str );
00138                 $total = 0;
00139 
00140                 // Count ASCII bytes
00141                 for( $i = 0; $i < 0x80; $i++ ) {
00142                         $total += $counts[$i];
00143                 }
00144 
00145                 // Count multibyte sequence heads
00146                 for( $i = 0xc0; $i < 0xff; $i++ ) {
00147                         $total += $counts[$i];
00148                 }
00149                 return $total;
00150         }
00151 
00160         public static function mb_strpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
00161                 $needle = preg_quote( $needle, '/' );
00162 
00163                 $ar = array();
00164                 preg_match( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE, $offset );
00165 
00166                 if( isset( $ar[0][1] ) ) {
00167                         return $ar[0][1];
00168                 } else {
00169                         return false;
00170                 }
00171         }
00172 
00181         public static function mb_strrpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
00182                 $needle = preg_quote( $needle, '/' );
00183 
00184                 $ar = array();
00185                 preg_match_all( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE, $offset );
00186 
00187                 if( isset( $ar[0] ) && count( $ar[0] ) > 0 &&
00188                         isset( $ar[0][count( $ar[0] ) - 1][1] ) ) {
00189                         return $ar[0][count( $ar[0] ) - 1][1];
00190                 } else {
00191                         return false;
00192                 }
00193         }
00194 }