Php determines whether it is a sample code of Chinese/English/numbers

Source: Internet
Author: User
Tags ereg

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 ');

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.