Php judges that the string is in pure English, pure Chinese characters, or a mix of Chinese and English formats.
- $ Strarray [1] = "hello ";
- $ Strarray [2] = "123456 ";
- $ Strarray [3] = "123 hello ";
- $ Strarray [4] = "script School ";
- $ Strarray [5] = "123 programmer's House ";
- $ Strarray [6] = "hello programmer's House ";
- $ Strarray [7] = "123hello programmer's House ";
-
- Foreach ($ strarray as $ key-> $ value)
- {
- $ X = mb_strlen ($ value, 'gb2312 ');
- $ Y = strlen ($ value );
-
- Echo $ strarray [$ key]. ''. $ x.''. $ y .'';
- }
-
- ?>
Running results: hello 5 5123456 6 6123 hello 8 Programmer's House 2 4123 programmer's house 5 7hello programmer's house 7 9123hello programmer's house 10 12 Php 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 lengths; GBK each Chinese character is equal to two lengths; 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
- /**
- * PHP judges whether the string is pure Chinese characters OR only English OR a mix of Chinese and English characters
- */
- Echo' ';
- 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 mixed ';
- }
-
- $ Str = 'blog ';
- Echo 'string: '. $ str.', which is '. 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 'Chinese characters ';
- Return 'Chinese-English mixed ';
- }
- ?>
|