Home >Backend Development >PHP Tutorial >PHP string length calculation - introduction to the use of strlen() function
strlen() function and mb_strlen() function
In PHP, the function strlen() returns the length of the string. The function prototype is as follows:
int strlen(string string_input);
The parameter string_input is the string to be processed.
strlen() function returns the length of bytes occupied by the string. An English letter, number, and various symbols all occupy one byte, and their lengths are all 1. A noon character occupies two bytes, so the length of a noon character is 2. For example
<?php echo strlen("www.php.cn"); echo strlen("PHP中文网"); ?>
The running result of "echo strlen("www.php.cn");": 15
The running result of "echo strlen("PHP Chinese Network");": 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:
int mb_strlen(string string_input, string encode);
PHP’s built-in string length function strlen cannot correctly handle Chinese strings. It only gets 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 Chinese strings:
<?php $str = "三知sunchis开发网"; echo strlen($str)."<br>"; //结果:22 echo mb_strlen($str,"UTF8")."<br>"; //结果:12 $strlen = (strlen($str)+mb_strlen($str,"UTF8"))/2; echo $strlen; //结果:17 ?>
Principle analysis:
strlen() When calculating, 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.
For more PHP string length calculation - introduction to the use of strlen() function, please pay attention to the PHP Chinese website for related articles!