This article is a detailed analysis of
The code is as follows |
Copy Code |
var reg =/^[ u4e00-u9fa5]+$/; If (reg.test (str)) { alert (' work on Chinese characters '); } $action = Trim ($_get[' action ')); if ($action = = "Sub") { $str = $_post[' dir ']; //if (!PR Eg_match ("/^[". Chr (0XA1). " -". Chr (0xff)." a-za-z0-9_]+$/", $str))//gb2312 alphanumeric underline regular expression if (!preg_match ("/^[x{4e00}-x{9fa5}a-za-z0-9_ ]+$/u ", $str)) //utf-8 Chinese characters alphanumeric underline regular expression { echo "<font color=red> you entered [". $str. "] Contains illegal characters </font> "; } Else { & nbsp; echo "<font color=green> you entered [". $str. "] Completely legal, through!</font> "; } } | "
Of course, if you want to match a string full of Chinese characters, the GBK2312 encoding matches:
The code is as follows |
Copy Code |
$str = "Keoko"; if (Preg_match ("/^[". Chr (0XA1). " -". Chr (0xFF)."] +$/", $str)) { Print ($str. " is really all Chinese characters "); } else { Print ($str. " This really TMD is not all Chinese characters "); } |
In fact, as long as we understand the high and low level of each coding start and end, then naturally can write regular, and direct is 16, what is the difficulty? Oh. Note, however, that in PHP, the 16-bit x is used.
So as above, we can also use the regular expression to determine whether the GB2312 Chinese characters
The code is as follows |
Copy Code |
<?php $str = "Keoko"; if (Preg_match ("/^[xb0-xf7][xa0-xfe]+$/", $str)) { Print ($str. " is really all Chinese characters "); } else { Print ($str. " This really TMD is not all Chinese characters "); } ?> |
In PHP combined with Preg_match ("/^[xb0-xf7][xa0-xfe]+$/", $str) can quickly realize the practice, there is a need for students to refer to.