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; } |