The difference between strlen and mb_strlen is strlenmb_strlen. The difference between strlen and mb_strlen is that strlenmb_strlen is a common function used in php to calculate the string length: strlen and mb_strlen. when the characters are all English characters, the two are the same. The difference between strlen and mb_strlen is as follows: strlenmb_strlen
Common functions used in php to calculate the string length include strlen and mb_strlen. when the characters are all English characters, the two are the same. Here we will mainly compare the two calculation results when mixing Chinese and English.
InPHPMedium,StrlenAndMb_strlenIt is a function to evaluate the length of a string, but for some beginners, if you do not read the manual, you may not know the difference.
The following is an example to illustrate the differences between the two.
First look at the example:
-
- // During the test, the file encoding method is UTF8.
- $ Str = 'Chinese character a 1 ';
- Echo strlen ($ str ).'
'; // 14
- Echo mb_strlen ($ str, 'utf8 ').'
'; // 6
- Echo mb_strlen ($ str, 'gbk ').'
'; // 8
- Echo mb_strlen ($ str, 'gb2312 ').'
'; // 10
- ?>
Result analysis: during strlen calculation, the Chinese character of UTF8 is 3 characters in length, so the length of "Chinese character a 1 character" is 3*4 + 2 = 14, when mb_strlen is calculated, if the selected inner code is UTF8, a Chinese character will be calculated as the length of 1. Therefore, the length of "Chinese a character 1 character" is 6.
The two functions can be used to calculate the placeholder value of a string in both Chinese and English (the placeholder value of a Chinese character is 2 and that of an English character is 1)
- Echo (strlen ($ str) + mb_strlen ($ str, 'utf8')/2;
For example, the strlen ($ str) value of "Chinese character a 1 character" is 14, and the mb_strlen ($ str) value is 6, the placeholder value of "Chinese character a 1 character" is 10.
- Echo mb_internal_encoding ();
The built-in string length function strlen in PHP cannot properly process Chinese strings. it only obtains the number of bytes occupied by 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 ).
Using the mb_strlen function can better solve this problem. The usage of mb_strlen is similar to that of strlen, except that it has a second optional parameter for specifying character encoding. For example, you can use mb_strlen ($ str, 'utf-8') to get the $ str length of the UTF-8 '). If the second parameter is omitted, the internal code of PHP is used. The internal encoding can be obtained through the mb_internal_encoding () function.
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 number of undefined functions may occur.
Difference between strlen and mb_strlen and count in php
Strlen calculates the string length. a Chinese character must be 2 characters long.
Mb_strlen uses its character encoding mode to calculate the character"
Count: calculates the number of elements in the array or the number of attributes in the object.
What are the roles of strlen () and mb_strlen?
In PHP, strlen and mb_strlen are functions used to evaluate the string length. However, for some beginners, if they do not read the manual, they may not know the difference.
The following is an example to illustrate the differences between the two.
First look at the example:
'; // 14 echo mb_strlen ($ str, 'utf8 ').'
'; // 6 echo mb_strlen ($ str, 'gbk ').'
'; // 8 echo mb_strlen ($ str, 'gb2312 ').'
'; // 10?>
Result analysis: during strlen calculation, the Chinese character of UTF8 is 3 characters in length, so the length of "Chinese character a 1 character" is 3*4 + 2 = 14, when mb_strlen is calculated, if the selected inner code is UTF8, a Chinese character will be calculated as the length of 1. Therefore, the length of "Chinese a character 1 character" is 6.
The two functions can be used to calculate the placeholder value of a string in both Chinese and English (the placeholder value of a Chinese character is 2 and that of an English character is 1)
Echo (strlen ($ str) + mb_strlen ($ str, 'utf8')/2;
For example, the strlen ($ str) value of "Chinese character a 1 character" is 14, and the mb_strlen ($ str) value is 6, the placeholder value of "Chinese character a 1 character" is 10.
Echo mb_internal_encoding ();
The built-in string length function strlen in PHP cannot properly process Chinese strings. it only obtains the number of bytes occupied by 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 ).
Using the mb_strlen function can better solve this problem. The usage of mb_strlen is similar to that of strlen, except that it has a second optional parameter for specifying character encoding. For example, you can use mb_strlen ($ str, 'utf-8') to get the $ str length of the UTF-8 '). If the second parameter is omitted, the internal code of PHP is used. The internal encoding can be obtained through the mb_internal_encoding () function.
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 number of undefined functions may occur.
In php, the common functions used to calculate the string length are strlen and mb_strlen. when the characters are all English characters, the two are the same. Here...