Php Regular Expression matches Chinese characters!
/^ [\ X {4e00}-\ x {9fa5}] + $/u
The above regular expression has plagued many php programmers.Regular Expression matching Chinese Characters
You may think it is very simple. In fact, different codes and different programming languages are slightly different, and you will not be able to get the correct results if you do not pay attention to them.
The following is an example of UTF-8 encoding:
$ Str="Chinese characters";
If (Preg_match("/^ [\ X {4e00}-\ x {9fa5}] + $/u",$ Str)){
Print ("This string is all Chinese");
} Else {
Print ("Not all strings are Chinese");
}
The following example containsExample:
<? Php
$ Action=Trim($ _ GET['Action']);
If ($ Action="Sub")
{
$ Str=$ _ POST['Dir'];
// If (! Preg_match ("/^ [". chr (0xa1 ). "-". chr (0xff ). "A-Za-z0-9 _] + $/", $ str) // GB2312 Regular Expression of Chinese characters, letters, numbers, underscores (_)
If (!Preg_match("/^ [\ X {4e00}-\ x {9fa5} A-Za-z0-9 _] + $/u",$ Str))// UTF-8, Chinese characters, letters, numbers, underscores, regular expressions
{
Echo"<Font color = red> [".$ Str."] Contains illegal characters </font>";
}
Else
{
Echo"<Font color = green> [".$ Str."] Completely legal, pass! </Font>";
}
}
?>
<Form method = "POST" action = "? Action = sub ">
Input characters (numbers, letters, Chinese characters, and underlines ):
<Input type = "text" name = "dir" value = "">
<Input type = "submit" value = "submit">
</Form>