PHP Regular Expression judgment Chinese UTF-8 & GBK

Source: Internet
Author: User

UTF-8 matching: In JavaScript, it is very easy to judge that the string is Chinese. 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 ");
}
It seems that no error is reported, and the result is correct. However, if you replace $ STR with "programming", the result still shows "Not all the strings are Chinese ", it seems that such judgment is not accurate enough. Important: I checked the <proficient Regular Expression> and found that for [\ x4e00-\ x9fa5], I made an enhanced explanation of PHP's regular expression, [\ x4e00-\ x9fa5] is actually the concept of character and character group. \ x {hex} represents a hexadecimal number, note that hex can be 1-2 bits or 4 bits, but if it is 4 bits, it must be enclosed by braces. If it is a hex larger than X {ff, must be used with the U modifier. Otherwise, an error will occur.
Only regular expressions matching the full-width characters can be found on the Internet: ^ [\ X80-\ xFF] * ^/. brackets are not added here.
[\ U4e00-\ u9fa5] can match Chinese characters, but PHP does not support
However, since \ x represents the hexadecimal data, why is the range \ x4e00-\ x9fa5 different from that provided in JS? So I switched to the following code and found that it was really accurate:
$ STR = "PHP programming ";
If (preg_match ("/^ [\ x {4e00}-\ x {9fa5}] + $/u", $ Str )){
Print ("all strings are Chinese ");
} Else {
Print ("Not all strings are Chinese ");
} The final correct expression for matching Chinese characters using regular expressions in PHP in UTF-8 encoding --/^ [\ x {4e00}-\ x {9fa5}] + $/u,
Refer to the aboveArticle I wrote the following test code (copy the following code and save it as a. php file) <? 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> The [". $ Str. "] You entered contains illegal characters </font> ";
}
Else
{
Echo "<font color = green> The [". $ Str. "] You entered is 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> GBK: preg_match ("/^ [". CHR (0xa1 ). "-". CHR (0xff ). "A-Za-z0-9 _] + $/", $ Str); // Regular Expression of gb2312 Chinese characters, letters, numbers, underscores.

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.