Home >Backend Development >PHP Tutorial >How to get the length of a string in PHP
Getting the length of a string is implemented using the strlen() function.
The syntax is as follows:
int strlen(string str)
Test:
$str="123abc ,.;"; echo strlen($str);
10
$str="107网站工作室"; echo strlen($str);
18
There will be questions at this time, a total of 8 characters , why the displayed length is 18, because in the case of UTF-8 encoding, three characters are one Chinese character, 3+5*3=18;
Then the question is, can the number of characters in the string be displayed? Well, for example, 8 in this example
At this time, a simple way is 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 result of running is as follows
8
mixedmb_strlen ( string$str
[, string$encoding
= mb_internal_encoding() ] )
The encoding
parameter is the character encoding. If omitted, the internal character encoding is used.
The above introduces how to get the length of a string in PHP, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.