The method of using PHP function to calculate the length of Chinese and English strings,
This paper describes the method of using PHP function to calculate the length of Chinese and English strings. Share to everyone for your reference. The implementation method is as follows:
In general, you know the English characters accounted for a byte, while the Chinese characters gbk two characters, utf8 three characters, many people think that the PHP string length is the strlen () function, in fact, it calculates the length of the byte rather than the length of the character, So how do you get the length of a character in a string? There are also Mb_strlen ().
The specific code is as follows:
Copy the Code code as follows: Echo $str = ' php dot-pass ';
echo strlen ($STR); 3*1+3*3=12
Echo Mb_strlen ($str, ' gb2312 '); 3*1+3*2=9
Echo Mb_strlen ($str, ' utf-8 '); 6
Damn, the MB series of functions is not the PHP core function, the default is not open, there is a super-simple method, through the regular string decomposition into individual characters, the number of characters is the length of the string, the code is as follows:
Copy the Code code as follows: <?php
function _strlen ($STR)
{
Preg_match_all ("/./us", $str, $matches);
Return count (current ($matches));
}
echo _strlen ("PHP dot-pass"); 6
?>
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/909334.html www.bkjia.com true http://www.bkjia.com/PHPjc/909334.html techarticle using PHP function to calculate the length of Chinese and English strings, this paper describes the method of using PHP function to calculate the length of Chinese and English strings. Share to everyone for your reference. Concrete implementation of the party ...