PHP judges that the string is pure English, pure Chinese characters, or a mix of Chinese and English characters.

Source: Internet
Author: User
Tags chr mixed php programming

English only

The code is as follows: Copy code

If (preg_match ("/^ [a-z] * $/I", "fdsFDfd "))

Echo "all letters

";

 } 

Pure Chinese characters

The code is as follows: Copy code

For example:
Var str = "php programming ";
If (/^ [u4e00-u9fa5] + $/. test (str )){
Alert ("all strings are Chinese ");
}
Else {
Alert ("Not all strings are Chinese ");
} In php, x is used to represent hexadecimal data. Therefore, it is transformed into the following code:
$ Str = "php programming ";
If (preg_match ("/^ [x4e00-x9fa5] + $/", $ str )){
Print ("all strings are Chinese ");
} Else {
Print ("Not all strings are Chinese ");
}


Chinese-English hybrid
 
Username: The username can contain lowercase letters, Chinese characters, numbers, and underscores (_). The username cannot contain only numbers, but cannot contain underscores (_).

The code is as follows: Copy code

/^ [A-z0-9_u4e00-u9fa5] + [^ _] $/g UTF-8

Preg_match ("/^ [a-z0-9_x80-xff] + [^ _] $/g", $ a); under gbk:

Preg_match ("/^ [a-z0-9 _". chr (0xa1). "-". chr (0xff). "] + [^ _] $/", $)


UTF-8 encoding

The code is as follows: Copy code


<? Php
/**
* PHP judges whether the string is pure Chinese characters OR only English OR a mix of Chinese and English characters
* Qiongtai blog
*/
Echo '<meta charset = "UTF-8"/> ';
Function utf8_str ($ str ){
$ Mb = mb_strlen ($ str, 'utf-8 ');
$ St = strlen ($ str );
If ($ st = $ mb)
Return 'English only ';
If ($ st % $ mb = 0 & $ st % 3 = 0)
Return 'Chinese characters ';
Return 'Chinese-English mixed ';
}
 
$ Str = 'qiongtai blog ';
Echo 'string: <span style = "color: red"> '. $ str. '</span>, which is <span style = "color: red"> '. utf8_str ($ str ). '</span> ';
?>

Gbk encoding

The code is as follows: Copy code

Function gbk_str ($ str ){
$ Mb = mb_strlen ($ str, 'gbk ');
$ St = strlen ($ str );
If ($ st = $ mb)
Return 'English only ';
If ($ st % $ mb = 0 & $ st % 2 = 0)
Return 'Chinese characters ';
Return 'Chinese-English mixed ';
}

Numbers, Chinese characters

The code is as follows: Copy code

$ Username = $ _ REQUEST ['username'];
If (! Preg_match ("/^ [a-z0-9xa1-xff] {} $/", $ username ))
 {
Echo "34r345 ";
Exit;
 }

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.