Home > Article > Backend Development > How to get the length of a string in PHP
Get the string length using the strlen() function.
The syntax is as follows:
int strlen(string str)
Test:
$str="123abc ,.;"; echo strlen($str);
The results are as follows
10
$str="107网站工作室"; echo strlen($str);
The results are as follows:
18
There will be questions at this time. There are 8 characters in total. Why is the displayed length 18? Because in the case of UTF-8 encoding, three characters are one Chinese character, 3+ 5*3=18;
Then the question is, can we display the number of characters in the string, such as 8 in this example
At this time, there is a simple way That is, you need to extend php.ini, remove the semicolon in front of extension=php_mbstring.dll, and then restart Apache
$str="107网站工作室"; echo mb_strlen($str,"UTF-8");
The operation will display the following results
8
mixed mb_strlen ( string $str
[, string $encoding
= mb_internal_encoding() ] )
encoding
The parameter is the character encoding. If omitted, the internal character encoding is used.
The above is the detailed content of How to get the length of a string in PHP. For more information, please follow other related articles on the PHP Chinese website!