Note: If the fatal error:call to undefined function Mb_strlen appears with Mb_strlen, you can use info () to see if there is no load mbstring, if not, try to Php_ Mbstring.dll Copy to the%windows% directory
File Encoding Utf-8
$var = ' Chinese characters abc ';
Mb_strlen ($var, ' utf-8 '); Output 7 Chinese and English are one byte
Mb_strlen ($var); Output 15 Chinese to 3 bytes English takes one byte
Mb_strlen ($var, ' GBK '); Output 9 not normal
Take all Chinese
function Utf8substr ($str, $from, $len) {return preg_replace (' #^ (?: [x00-x7f]|[ xc0-xff][x80-xbf]+) {0, '. $from. '} '. ' (?: [x00-x7f]| [Xc0-xff] [x80-xbf]+) {0, '. $len. '}). * #s ', ' $ ', $str); }
Chinese and English mixed body intercept code
function Gb2312_strlen ($string) {$str _len = strlen ($string); $str _count = 0;for ($j = 0; $j < $str _len; $j + +) { if (ord ($string {$j}) < 127) { $str _count + = 1; Continue; } else { if (ord ($string {$j +1}) > 127) { $str _count + = 1; $j + +; Continue; } else { $str _count + = 1; Continue;}}} return $str _count;} $STR = "Open s D"; echo Gb2312_strlen ($STR);