Php judges Chinese language, English, and numbers

Source: Internet
Author: User
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 ');


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.