-
- $strarray[1] = "hello";
- $strarray[2] = "123456";
- $strarray[3] = "123hello";
- $strarray[4] = " Scripting School";
- $strarray[5] = "123 Programmer's Home";
- $strarray[6] = "hello Programmer's Home";
- $strarray[7] = "123hello Programmer's Home";
-
- foreach ($strarray as $key->$value)
- {
- $x = mb_strlen($value,'gb2312');
- $y = strlen($value);
-
- echo $strarray[$key]. ' '.$x.' '.$y.' }
-
- ?>
Copy code
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
-
- /**
- * PHP determines whether the string is pure Chinese characters OR pure English OR mixed Chinese and English
- */
- echo '';
- function utf8_str($str){
- $mb = mb_strlen ($str,'utf-8');
- $st = strlen($str);
- if($st==$mb)
- return 'Pure English';
- if($st%$mb==0 && $st%3==0)
- return 'Pure Chinese';
- return 'Chinese-English Mixed';
- }
-
- $str = 'Blog';
- echo 'String: '.$str.', is '.utf8_str($str).'';
- ?>
Copy code
GBK method
-
- function gbk_str($str){
- $mb = mb_strlen($str,'gbk');
- $st = strlen($str);
- if($st==$ mb)
- return 'Pure English';
- if($st%$mb==0 && $st%2==0)
- return 'Pure Chinese characters';
- return 'Mixed Chinese and English';
- }
- ?>
Copy code
|