#EXAMPLE
$str _uncode = "Simplified Chinese Chinese (Simplified)";//statistical string length echo strlen ($str _uncode). ' <br> ';//Result: 31echo Mb_strlen ($str _uncode, ' UTF-8 '). ' <br> '; Results: 23echo Mb_strlen ($str _uncode, ' GBK '). ' <br> '; Results: 25echo Mb_strlen ($str _uncode, ' GB2312 '). ' <br> '; result 27
Results Analysis:
Strlen a Chinese by 3 bytes (complex Chinese characters will be counted as 4 bytes)
Mb_strlen ' UTF-8 ' encodes a Chinese character to be counted by a byte bit
PHP built-in string length function strlen cannot handle Chinese strings correctly, it only gets the number of bytes that the string occupies. For the Chinese encoding of GB2312, strlen get the value is twice times the number of Chinese characters, and for UTF-8 encoded in Chinese, is 3 times times the difference (in UTF-8 encoding, a Chinese character accounted for 3 bytes).
Using Mb_strlen function can solve this problem well. The usage of Mb_strlen is similar to strlen, except that it has a second optional parameter for specifying character encoding. For example get UTF-8 string $str length, can be used Mb_strlen ($str, ' UTF-8 ').
If you omit the second argument, the internal encoding of PHP is used. The internal code can be obtained through the mb_internal_encoding () function.
Mb_strlen is not a PHP core function, you need to make sure to load the Php_mbstring.dll in php.ini, that is, to ensure that the "Extension=php_mbstring.dll" line exists and is not commented out, otherwise there will be undefined letter Number of questions.
PHP built-in functions strlen and mbstring extension function Mb_strlen differences