However, it comes with PHP In the function, strlen and mb_strlen both calculate the length by calculating the number of bytes occupied by the string. Under different encoding conditions, the number of bytes occupied by Chinese is different. Under GBK/GB2312, Chinese characters occupy 2 bytes, while under UTF-8, Chinese characters occupy 3 bytes.
And we often need to judge when judging the length of a string It is the number of characters, not the number of bytes occupied by the string, such as this php code under UTF-8:
So there is What convenient and practical method can be used to obtain the length of a string containing Chinese characters? You can use regular rules to calculate the number of Chinese characters, divide by 2 under GBK/GB2312 encoding, divide by 3 under UTF-8 encoding, and finally add the length of the non-Chinese string, but this is too troublesome, WordPress There is a more beautiful piece of code in , which is as follows:
Use regular expressions The formula splits the string into single characters, and directly uses count to calculate the number of matching characters, and then we get the result we want. |