Play PHP (a)-php processing Chinese character string length: strlen and mb_strlen note: This article is a small series of original, if reproduced, please indicate the source: http://blog.csdn.net/u012116457/article/details/42536039
I officially started learning PHP today, but I accidentally discovered something that requires special attention when using PHP.
For example:
The output result of this code is: name Length: 6
First, let's look at why this problem occurs: the built-in string length function strlen () in PHP cannot correctly process Chinese strings. what it returns is the number of bytes occupied by the strings. For GB2312 Chinese encoding, strlen obtains two times the number of Chinese characters, and for the UTF-8 encoding of Chinese, is three times the difference (in the UTF-8 encoding, A Chinese character occupies 3 bytes ).
If you want to use the strlen function to calculate the length of a string containing Chinese characters, the following example is taken online:
0xa0) { $tmpstr.=substr($str,$i,2); $i++; } else $tmpstr.=substr($str,$i,1); } return $tmpstr; }?>
However, apart from this, we can use the mb_strlen function to calculate:
The output result is: the name length is: 2
The usage of mb_strlen is similar to that of strlen, except that it has a second optional parameter for specifying character encoding.
It should be noted that mb_strlen is not a PHP core function. before using it, make sure that. the "php_mbstring.dll" line is loaded in ini to ensure that the "extension = php_mbstring.dll" line exists and is not commented out. Otherwise, the problem of undefined functions may occur.