This article describes how to match Chinese characters with regular expressions in php. For more information, see php regular expression matching!
/^ [\ 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:
$ 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"[".$ Str."] Contains illegal characters";
}
Else
{
Echo"[".$ Str."] Completely legal, pass! ";
}
}
?>