PHP determines whether a string is a Chinese character, a Chinese character, or a mix of Chinese and English PHP determines whether the string is a Chinese character (or English). In addition to regular expressions, does PHP determine whether the character value is less than 128? There is also a special method. It is relatively simple to use the mb_strlen and strlen functions in php: Use the above two functions to test the current encoding character. PHP judges that the string is a pure English, pure Chinese character, or a mix of Chinese and English characters.
PHP determines whether a string is a Chinese character (or English). In addition to regular expressions and split characters, PHP judges whether the character value is less than 128.
?
There is also a special method.
Use the mb_strlen and strlen functions in php to determine
The method is relatively simple: use the above two functions to determine the return value of the character with the current encoding, and then compare the return value.
The returned values are English-only, numbers-only, and English-numbers;
The return value varies, and the strlen return value can be divisible by mb_strlen into Chinese characters.
The return value is not equal, and the strlen return value cannot be divisible by mb_strlen in English-Chinese or Chinese-English mixing.
?
Take a look at the following example:
$ Value) {$ x = mb_strlen ($ value, 'gb2312'); $ y = strlen ($ value); echo $ strarray [$ key]. ''. $ x. ''. $ y. '';}?>?
The result after running is:
Hello 5 5
123456 6 6
123 hello 8 8
Hello 2 4
123 Hello 5 7
Hello, 7, 9
123hello, 10 12
?
Source: http://007blogchina.appspot.com /? P = 130001
?
HP does not have a direct function to determine whether a string is pure English, pure Chinese characters, and a mix of Chinese and English characters. you can only write functions by yourself. To implement this function, you must understand the character set encoding placeholder. Currently, UTF8 and GBK are commonly used character sets in China.
UTF8 each Chinese character is equal to three characters in length;
GBK is equal to two Chinese characters;
Using the differences between Chinese characters and English letters, we can use the mb_strlen function and the strlen function to calculate the two groups of length numbers, and then calculate the string type based on the regular operation.
?
UTF-8 instance
'; Function utf8_str ($ str) {$ mb = mb_strlen ($ str, 'utf-8'); $ st = strlen ($ str ); if ($ st = $ mb) return 'English only '; if ($ st % $ mb = 0 & $ st % 3 = 0) return 'Chinese characters'; return 'Chinese-English mixture ';} $ str = 'blog'; echo 'string :'. $ str. ', Yes '. utf8_str ($ str ). '';?>?
GBK method
Function gbk_str ($ str) {$ mb = mb_strlen ($ str, 'gbk'); $ st = strlen ($ str); if ($ st = $ mb) return 'English only '; if ($ st % $ mb = 0 & $ st % 2 = 0) return 'pure Chinese chars'; return 'Chinese-English mixture ';}?
Source: http://www.qttc.net/201207142.html
?
?
?