Php judges Chinese characters, English letters, numbers & nbsp; $ str = 'asb Tianshui 12'; & nbsp; & nbsp; if (preg_match (& quot;/^ [\ x7f-\ xff] & #43; $/& quot;, $ st php judges Chinese characters, English letters, and numbers
$ Str = 'asb Tianshui 12 ';
If (preg_match ("/^ [\ x7f-\ xff] + $/", $ str )){
Echo 'all Chinese characters ';
} Else {
Echo 'all Chinese characters ';
}
/** PHP determines whether it is Chinese. 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 'you entered a combination of Chinese and English numbers! '.'
'; 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 = 'English string of Chinese characters and numbers ';} elseif ($ a & $ B &&! $ C) {$ output = 'mixed string of Chinese characters and numbers ';} elseif ($ &&! $ B & $ c) {$ output = 'English Chinese character mixed string';} elseif (! $ A & $ B & $ c) {$ output = 'English-digit mixed string';} elseif ($ &&! $ B &&! $ C) {$ output = 'Chinese characters';} elseif (! $ A & $ B &&! $ C) {$ output = 'pure number';} elseif (! $ &&! $ B & $ c) {$ output = 'English only ';} return $ output;} echo checkStr ('5 love u ');