3 if (!function_exists('mb_strlen'))
6 // author Martin Kutschker <martin.t.kutschker@blackbox.net>
7 function mb_strlen($str, $encoding="utf-8")
9 if($encoding != "utf-8")
14 for($i=0; isset($str{$i}); $i++)
18 if (!($c & 0x80)) // single-byte (0xxxxxx)
21 elseif (($c & 0xC0) == 0xC0) // multi-byte starting byte (11xxxxxx)
29 if (!function_exists('mb_substr'))
32 // author Martin Kutschker <martin.t.kutschker@blackbox.net>
33 function mb_substr($str, $start, $len=null, $encoding="utf-8")
35 if($encoding != "utf-8")
38 $byte_start = utf8_char2byte_pos($str,$start);
40 if ($byte_start === false)
41 return false; // $start outside string length
43 $str = substr($str,$byte_start);
47 $byte_end = utf8_char2byte_pos($str,$len);
49 if ($byte_end === false) // $len outside actual string length
52 return substr($str,0,$byte_end);
58 function utf8_char2byte_pos($str,$pos)
60 $n = 0; // number of characters found
61 $p = abs($pos); // number of characters wanted
72 for( ; isset($str{$i}) && $n<$p; $i+=$d)
74 $c = (int)ord($str{$i});
76 if (!($c & 0x80)) // single-byte (0xxxxxx)
78 elseif (($c & 0xC0) == 0xC0) // multi-byte starting byte (11xxxxxx)
83 return false; // offset beyond string length
87 // skip trailing multi-byte data bytes
88 while ((ord($str{$i}) & 0x80) && !(ord($str{$i}) & 0x40))