Copy codeThe Code is as follows:
$ Str = 'asb Tianshui 12 ';
If (preg_match ("/^ [\ x7f-\ xff] + $/", $ str )){
Echo 'all Chinese characters ';
} Else {
Echo 'all Chinese characters ';
}
/**
PHP determines whether it is a Chinese character,
Eregi ('[^ \ x00-\ x7F]', $ str) // Chinese
Eregi ('[0-9]', $ str) // number
Eregi ('[a-zA-Z]', $ str) // english
*/
If (eregi ('[^ \ x00-\ x7F]', $ str) | eregi ('[0-9]', $ str) | eregi ('[a-zA-Z]', $ str )){
Echo: What you entered is a combination of Chinese and English numbers! '.' <Br> ';
Echo "Length:". strlen ($ str );
}
/**
The following two methods are used to determine whether a string is composed of English characters and numbers,
Or the variable $ str for a string consisting of Chinese characters or the variable starting with this document
*/
If (preg_match_all ("/^ ([\ x81-\ xfe] [\ x40-\ xfe]) + $/", $ str, $ match )){
Echo 'all Chinese characters ';
} Else {
Echo 'all Chinese characters ';
}
If (preg_match ("/([\ x81-\ xfe] [\ x40-\ xfe])/", $ str, $ match )){
Echo 'contains Chinese characters ';
} Else {
Echo 'contains no Chinese characters ';
}
/**
This is the js method, judging that a Chinese character occupies two bytes, a Chinese character or number occupies one, using the encoding for the UTF-8
*/
<Script>
Var leng = {};
Var value = document. forms [0]. name. value;
Jmz. GetLength = function (str ){
Var realLength = 0, len = str. length, charCode =-1;
For (var I = 0; I <len; I ++ ){
CharCode = str. charCodeAt (I );
If (charCode> = 0 & charCode <= 128) realLength + = 1;
Else realLength + = 2;
}
Return realLength;
};
Alert (leng. GetLength (value ))
</Script>
Function checkStr ($ str ){
$ Output = '';
$ A = ereg ('['. chr (0xa1). '-'. chr (0xff). ']', $ str );
$ B = ereg ('[0-9]', $ str );
$ C = ereg ('[a-zA-Z]', $ str );
If ($ a & $ B & $ c) {$ output = 'mixed string of Chinese characters and numbers in English ';}
Elseif ($ a & $ B &&! $ C) {$ output = 'mixed string of Chinese characters and numbers ';}
Elseif ($ &&! $ B & $ c) {$ output = 'Chinese and English character strings ';}
Elseif (! $ A & $ B & $ c) {$ output = 'English-digit mixed string ';}
Elseif ($ &&! $ B &&! $ C) {$ output = 'pure Chinese characters ';}
Elseif (! $ A & $ B &&! $ C) {$ output = 'pure number ';}
Elseif (! $ &&! $ B & $ c) {$ output = 'English only ';}
Return $ output;
}
Echo checkStr ('5 love U ');