Php regular expression matching Chinese problem analysis summary
Source: Internet
Author: User
This article analyzes the question of support for regular expressions in php for Chinese characters. In general, we can simply use the regular expressions above to verify Chinese characters. later I analyzed compatibility with gb2312, UTF-8 and expression ^ [\ x7f-\ xff] + $ can completely solve Chinese incompatibility issues
The code is as follows:
$ Str = '123456789abcdefg of the People's Republic of China ';
Echo preg_match ("/^ [u4e00-u9fa5_a-zA-Z0-9] {3, 15} $", $ strName );
Run the above code to see the prompt information?
Warning: preg_match (): Compilation failed: PCRE does not support L, l, N, P, p, U, or X at offset 3 in F: wwwrootphptest. php on line 2
Originally, PHP regular expressions do not support the following Perl escape sequences: L, l, N, P, p, U, u, or X
In UTF-8 mode, "x {...}" is allowed, and the content in braces is a string that represents a hexadecimal number.
The original hexadecimal escape sequence xhh matches a dubyte UTF-8 character if its value is greater than 127.
So,
It can be solved in this way.
The code is as follows:
Preg_match ("/^ [x80-xff_a-zA-Z0-9] {} $", $ strName );
Preg_match ('/[x {2460}-x {2468}]/U', $ str );
Match Chinese characters with internal code
The code is as follows:
The code is as follows:
$ Str = "php programming ";
If (preg_match ("/^ [x {2460}-x {2468}] + $/u", $ str )){
Print ("all strings are Chinese ");
} Else {
Print ("Not all strings are Chinese ");
}
I found that this time I was still judging whether it was a Chinese character. However, since x represents the hexadecimal data, why is it different from the range x4e00-x9fa5 provided in js? So I changed to the following code:
The code is as follows:
$ Str = "php programming ";
If (preg_match ("/^ [x4e00-x9fa5] + $/u", $ str )){
Print ("all strings are Chinese ");
} Else {
Print ("Not all strings are Chinese ");
}
I thought it was a success. I did not expect that warning once again produced:
Warning: preg_match () [function. preg-match]: Compilation failed: invalid UTF-8 string at offset 6 in test. php on line 3
There seems to be another wrong expression, so I compared the expression in the article and wrapped it in "{" and "}" for "4e00" and "9fa5" respectively, I ran it again and found it was really accurate:
The code is as follows:
$ 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,
Summary:
The code is as follows:
// If (preg_match ("/^ [". chr (0xa1 ). "-". chr (0xff ). "] + $/", $ str) {// can only be used in the case of GB2312
If (preg_match ('// ^ [x7f-xff] + $/", $ str) {// Compatible with gb2312, UTF-8
Echo "correct input ";
} Else {
Echo "incorrect input ";
}
Double byte character encoding range
1. GBK (GB2312/GB18030)
X00-xff GBK dubyte encoding range
X20-x7f (ASCII)
Xa1-xff Chinese gb2312
X80-xff Chinese gbk
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.