We all know that English characters take up one byte, while Chinese characters gbk take up two, and utf8 take up three. many people think that php calculates the string length as the strlen () function, it calculates the length of a byte rather than the length of a character.
We all know that English characters take up one byte, while Chinese characters gbk take up two, and utf8 take up three. many people think that php calculates the string length as the strlen () function, it calculates the length of bytes rather than the length of characters. how can we get the length of characters in a string? There is also mb_strlen ()
Echo $ str = 'php point 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
Sadly, the mb series functions are not the core functions of PHP and are not enabled by default. There is also a super simple method to break strings into individual characters through regular expressions, the number of characters is the length of the string: