UTF-8 Chinese regular expression, UTF-8 Chinese Regular Expression

Source: Internet
Author: User

[Turn] UTF-8 Chinese Character regular expression, UTF-8 Chinese Character Regular Expression

Link: http://blog.csdn.net/wide288/article/details/30066639

 

$ Str = "programming ";
// If (! Preg_match ("/^ [\ x {4e00}-\ x {9fa5} A-Za-z0-9 _] + $/u", $ str) // UTF-8 Chinese characters, letters, numbers, underscores, regular expressions
If (! Preg_match ("/^ [\ x {4e00}-\ x {9fa5}] + $/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> ";

}

-----------------------

UTF-8 match:
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, after changing $ str to "programming", the result still shows that "not all strings are Chinese". It seems that such a judgment is not accurate enough.
Important: I checked <proficient in regular expressions> and found that I made an enhanced explanation for [\ x4e00-\ x9fa5 ].
In php regular expressions, [\ 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, braces must be added,
At the same time, if it is a hex greater than x {FF}, it must be used with the u modifier. Otherwise, an error will occur.

Only regular expressions matching full-width characters can be found on the Internet: ^ [\ x80-\ xff] * ^/. brackets [\ u4e00-\ u9fa5] can be added here to match Chinese characters, however, PHP does not support it. Why is the range \ x4e00-\ x9fa5 different from the hexadecimal data in \ x? 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 ");}
I understand the final correct expression for matching Chinese characters with regular expressions in UTF-8 encoding in php --/^ [\ x {4e00}-\ x {9fa5}] + $/u, refer to the above article and write the following test code (copy the following code and save it. 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 Chinese characters, letters, numbers, underscores, regular expressions 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>

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.