Home >Backend Development >PHP Tutorial >PHP determines whether a string is pure English, pure Chinese characters, or a mixed format of Chinese and English
operation result: hello 5 5 123456 6 6 123hello 8 8 Programmer's Home 2 4 123 Programmer’s Home 5 7 hello programmer home 7 9 123hello Programmer’s Home 10 12 PHP does not have a direct function to determine whether a string is pure English or pure Chinese characters or a mixture of Chinese and English. You can only write the function yourself. In order to realize this function, it is necessary to understand the Chinese character encoding occupancy of the character set. At present, the more commonly used character sets in China are UTF8 and GBK. Each Chinese character in UTF8 is equal to 3 lengths; Each Chinese character in GBK is equal to 2 lengths; Using the above differences between Chinese characters and English, we can use the mb_strlen function and strlen function to calculate two sets of length numbers respectively, and then perform operations according to the rules to determine the type of the string. UTF-8 example
GBK method
|