PHP determines whether a string is a Chinese (or English) method, except that the regular expression determines and splits the character to determine whether the value of the character is less than 128
There is a more special method.
use the Mb_strlen and strlen functions in PHP to determine
The method is simple: Use the above two functions to measure the return value of the character in the current encoding, and then compare the return value.
The return value is equal to pure English, pure digital, English number mixed;
The return value is unequal, and the strlen return value can be mb_strlen divisible by the pure Chinese character
The return value is not equal, and the Strlen return value is not divisible by mb_strlen into English or Chinese mixed rows
Take a look at the following example:
PHP code
- <?php
- $strarray [1] = "Hello";
- $strarray [2] = "123456";
- $strarray [3] = "123hello";
- $strarray [4] = "Hello";
- $strarray [5] = "123 Hello";
- $strarray [6] = "Hello hi";
- $strarray [7] = "123hello Hello";
- foreach ($strarray as $key,$value)
- {
- $x = Mb_strlen ($value,' gb2312 ');
- $y = strlen ($value);
- echo $strarray [$key].' <span style= "color: #ff0000;" > '. $x.' </span> <span style= ' color: #ff0000; " > '. $y.' </span> ';
- }
- ?>
The result after the run is:
Hello 5 5
123456 6 6
123hello 8 8
Hello 2 4
123 Hello 5 7
Hello Hi 7 9
123hello Hello 10 12
Source: http://007blogchina.appspot.com/?p=130001
HP does not have a direct function to determine whether a string is pure English or pure Chinese and Chinese-English mixed and can only write its own function. In order to realize this function, it is necessary to understand the character set Chinese character coding placeholder, the current domestic more commonly used character sets are UTF8 and GBK.
UTF8 each Chinese character equals 3 length;
GBK each Chinese character equals 2 length;
Using the differences between Chinese characters and English, we can use Mb_strlen function and strlen function to calculate two sets of length numbers respectively, and then to determine the type of string according to the law.
UTF-8 instances
PHP code
- <?php
- /**
- * PHP judgment String Pure Chinese or plain English or Chinese-English mixed
- */
- echo ' <meta charset= ' utf-8 '/> ';
- function Utf8_str ($str) {
- $MB = Mb_strlen ($str,' utf-8 ');
- $st = strlen ($str);
- if ($st = =$mb)
- return ' plain English ';
- if ($st%$mb ==0 && $st%3==0)
- return ' pure Chinese characters ';
- return ' Chinese-English mix ';
- }
- $str = ' blog ';
- Echo ' string: <span style= "color:red" > ". $str.' </span> <span style= ' color:red ' > '. UTF8_STR ($str).' </span> ';
- ?>
Gbk method
PHP code
- function Gbk_str ($str) {
- $MB = Mb_strlen ($str,' GBK ');
- $st = strlen ($str);
- if ($st = =$mb)
- return ' plain English ';
- if ($st%$mb ==0 && $st%2==0)
- return ' pure Chinese characters ';
- return ' Chinese-English mix ';
- }
Source: http://www.qttc.net/201207142.html