strlen() function and mb_strlen() function
In PHP, the function strlen() returns the length of the string. The function prototype is as follows:
Copy code The code is as follows:
int strlen(string string_input);
The parameter string_input is the string to be processed.
The strlen() function returns the byte length occupied by the string. An English letter, number, and various symbols all occupy one byte, and their length is 1. A noon character occupies two bytes, so the length of a noon character is 2. For example,
Copy code The code is as follows:
echo strlen("www.sunchis.com" ;
The running result of "echo strlen("三知dev网");": 15
There is a question here, doesn't a Chinese character occupy 2 bytes? "Sanzhi Development Network" clearly has five Chinese characters, so how could the result be 15?
The reason is here: when calculating strlen(), for a UTF-8 Chinese character, it will be treated as having a length of 3. When there is a mix of Chinese and English, how to accurately calculate the length of the string? Here, another function mb_strlen() must be introduced. The usage of the mb_strlen() function is almost the same as strlen(), except that there is an additional parameter that specifies the character set encoding. The function prototype is:
Copy code
The code is as follows:
int mb_strlen(string string_input, string encode);
PHP’s built-in string length function strlen cannot handle Chinese strings correctly. All it gets is the number of bytes occupied by the string. For GB2312 Chinese encoding, the value obtained by strlen is twice the number of Chinese characters, while for UTF-8 encoded Chinese, the difference is three times (under UTF-8 encoding, one Chinese character occupies 3 bytes). Therefore, the following code can accurately calculate the length of the Chinese string:
Copy code
The code is as follows:
php $str = "三知sunchisDevelopmentNetwork"; echo strlen($str)."
"; //Result: 22
echo mb_strlen($str,"UTF8") ."
"; //Result: 12
$strlen = (strlen($str)+mb_strlen($str,"UTF8"))/2;
echo $strlen; //Result: 17
?>
Principle analysis:
When calculating strlen(), the length of Chinese characters treated in UTF-8 is 3, so " The length of "Sanzhi Sunchis Development Network" is 5×3+7×1=22
When calculating mb_strlen, if the internal code is selected as UTF8, a Chinese character will be calculated as a length of 1, so "Sanzhi "sunchis Development Network" has a length of 5×1+7×1=12
The rest is a pure mathematical problem, so I won’t go into details here...
Note:
For mb_strlen($str,'UTF-8'), if the second parameter is omitted, PHP's internal encoding will be used. The internal encoding can be obtained through the mb_internal_encoding() function. It should be noted that mb_strlen is not a core function of PHP. Before using it, you need to make sure that php_mbstring.dll is loaded in php.ini, that is, make sure that the line "extension=php_mbstring.dll" exists and is not commented out, otherwise it will be undefined. function problem.
http://www.bkjia.com/PHPjc/824847.htmlwww.bkjia.com
truehttp: //www.bkjia.com/PHPjc/824847.htmlTechArticlestrlen() function and mb_strlen() function In PHP, the function strlen() returns the length of the string. The function prototype is as follows: Copy code The code is as follows: int strlen(string string_input); Parameter string...