Strlen-get string Length description intstrlen (string $ string) returns the length of the given string. The length of the string parameter. If the return value is successful, the length of the string is returned. If the return value is null, the return value is 0. Mb_strlen-get the length description of the string mixedmb
Strlen-get string Length description int strlen (string $ string) returns the length of the given string. The length of the string parameter. If the return value is successful, the length of the string is returned. If the return value is null, the return value is 0. Mb_strlen-get the length description of the string mixed mb
Strlen-get the string length
Description
IntStrlen(String$string
)
Returns the given string.string
.
Parameters
-
string
The string to be calculated.
Return Value
Returns a string.string
Length; ifstring
If it is null, 0 is returned.
Mb_strlen-get the string length
Description
MixedMb_strlen(String$str
[, String$encoding
= Mb_internal_encoding ()])
Returns the length of a string.
Parameters
-
str
The string to be checked.
-
encoding
encoding
The parameter is character encoding. If omitted, internal character encoding is used.
Return Value
Returnsencoding
Encoded stringstr
The number of characters. Multi-byte characters are counted as 1.
Ifencoding
InvalidFALSE
.
The above is the explanation of the two functions in the manual. At first glance, it seems that they are similar.
The following example describes the differences between the two.
// The file encoding method is UTF8 $ str = 'character a, string B, 2 degrees long '; echo strlen ($ str ).'
'; // 19 echo mb_strlen ($ str, 'utf-8 ').'
'; // 9
Let's look at the next example.
'; // 14 echo mb_strlen ($ str, 'utf-8 ').'
'; // 9?>
Result Analysis: When the strlen character length is reached, the Chinese character of UTF8 is counted as three characters in length,
Therefore, the length of "word a character 1 string B length 2 degrees" is 3 + 1 + 3 + 1 + 3 + 1 + 3 + 1 + 3 = 19,
When mb_strlen is calculated, if the selected inner code is UTF8, a Chinese character is regarded as 1 in length. Therefore, the length of "character a, string B, length 2 degrees" is 9.
In gb2312, when strlen calculates the character length, a Chinese character is counted as two characters.
In addition, it should be noted that mb_strlen is not a PHP core function. before using it, make sure to load php_mbstring.dll in php. ini; otherwise, the number of undefined functions may occur.