Does php determine whether it is a Chinese regular expression ?? Www.cnblogs.comdavid yanarticles2032115.html ?? Does php determine whether it is a Chinese regular expression? & Lt ;? Php $ straaa; if (! Eregi ([^ x80-xff], $ str) {echo is;} else {echo not php judge whether it is a Chinese regular expression Daquan (turn)
Reprinted from ?? Http://www.cnblogs.com/DavidYan/articles/2032115.html
?
?
Php judge whether it is a Chinese regular expression
?
$ Str = "aaa ";
If (! Eregi ("[^ \ x80-\ xff]", "$ str "))
{
Echo "yes ";
}
Else
{
Echo "not ";
}
?>
$ Str = "China ";
Echo $ str;
Echo "";
// 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 ";
}
?>
To determine whether a string contains Chinese characters, use the following code:
If (preg_match ("/[\ x7f-\ xff]/", $ string )){
Echo "has Chinese characters ";
} Else {
Echo "no Chinese ";
}
Determine whether the Chinese character is related to the encoding. if the gbk is a double byte and the utf8 is a triple byte, you can determine the value based on the Chinese character range ???
?
The encoding range is 1. GBK (GB2312/GB18030 )???
\ X00-\ xffGBK dubyte encoding range ???
\ X20-\ x7fASCII ???
\ Xa1-\ xff Chinese ???
\ X80-\ xff Chinese ??
??
2. UTF-8 (Unicode )???
\ U4e00-\ u9fa5 (Chinese )???
\ X3130-\ x318F (Korean ???
\ XAC00-\ xD7A3 (Korean )???
\ U0800-\ u4e00 (Japanese )???
Ps: Korean is a character greater than [\ u9fa5 ???
Regular example :???
Preg_replace ("/([\ x80-\ xff])/", "", $ str );??
Preg_replace ("/([u4e00-u9fa5])/", ", $ str );???
?
?
- // Determine whether the content contains Chinese characters-GBK? (PHP )???????
- Function? Check_is_chinese ($ s ){???????
- ?????Return? Preg_match ('/[\ x80-\ xff]./',? $ S );???????
- }???????
- ?????
- // Obtain the string length-GBK? (PHP )???????
- Function? Gb_strlen ($ str ){???????
- ????? $ Count? =? 0 ;???????
- ?????For($ I = 0 ;? $ I < li><>
- ????????? $ S? =? Substr ($ str ,? $ I ,? 1 );???????
- ?????????If? (Preg_match ("/[\ x80-\ xff]/",? $ S ))? + + $ I ;???????
- ??????????? + + $ Count ;???????
- ?????}???????
- ?????Return? $ Count ;???????
- }???????
- ?????
- // Intercept the string-GBK? (PHP )???????
- Function? Gb_substr ($ str ,? $ Len ){???????
- ????? $ Count? =? 0 ;???????
- ?????For($ I = 0 ;? $ I < li><>
- ?????????If($ Count? ==? $ Len )?Break;???????
- ?????????If(Preg_match ("/[\ x80-\ xff]/",? Substr ($ str ,? $ I ,? 1 )))? + + $ I ;???????
- ??????????? + + $ Count ;???????????????
- ?????}???????
- ?????Return? Substr ($ str ,? 0 ,? $ I );???????
- }???????
- ?????
- // Count the length of the string-UTF8? (PHP )???????
- Function? Utf8_strlen ($ str )? {???????
- ????? $ Count? =? 0 ;???????
- ?????For($ I? =? 0 ;? $ I?
- ????????? $ Value? =? Ord ($ str [$ I]); ???
- ?????????If($ Value?>? (127 )? {???????
- ????????????? $ Count ++ ;???????
- ?????????????If($ Value?> =? 192? &&? $ Value? <=? (223 )? $ I ++ ;???????
- ?????????????Elseif($ Value?> =? 224? &&? $ Value? <=? (239 )? $ I? =? $ I? +? 2 ;???????
- ?????????????Elseif($ Value?> =? 240? &&? $ Value? <=? (247 )? $ I? =? $ I? +? 3 ;???????
- ?????????????Else?Die('Not? A? UTF-8? Compatible? String ');???????
- ?????????}???????
- ????????? $ Count ++ ;???????
- ?????}???????
- ?????Return? $ Count ;???????
- }???????
- ?????
- ?????
- // Truncate the string-UTF8 (PHP )???????
- Function? Utf8_substr ($ str, $ position, $ length ){???????
- ????? $ Start_position? =? Strlen ($ str );???????
- ????? $ Start_byte? =? 0 ;???????
- ????? $ End_position? =? Strlen ($ str );???????
- ????? $ Count? =? 0 ;???????
- ?????For($ I? =? 0 ;? $ I?
- ?????????If($ Count?> =? $ Position? &&? $ Start_position?>? $ I ){???????
- ????????????? $ Start_position? =? $ I ;???????
- ????????????? $ Start_byte? =? $ Count ;???????
- ?????????}???????
- ?????????If($ Count-$ start_byte)> = $ length )? {???????
- ????????????? $ End_position? =? $ I ;???????
- ?????????????Break;???????
- ?????????}???????????
- ????????? $ Value? =? Ord ($ str [$ I]); ???
- ?????????If($ Value?>? 127 ){???????
- ????????????? $ Count ++ ;???????
- ?????????????If($ Value?> =? 192? &&? $ Value? <=? (223 )? $ I ++ ;???????
- ?????????????Elseif($ Value?> =? 224? &&? $ Value? <=? (239 )? $ I? =? $ I? +? 2 ;???????
- ?????????????Elseif($ Value?> =? 240? &&? $ Value? <=? (247 )? $ I? =? $ I? +? 3 ;???????
- ?????????????Else?Die('Not? A? UTF-8? Compatible? String ');???????
- ?????????}???????
- ????????? $ Count ++ ;???????
- ?????
- ?????}???????
- ?????Return(Substr ($ str, $ start_position, $ end_position-$ start_position ));???????
- }???????
- ?????
- // Determine whether there is a Korean-UTF-8? (JavaScript )???????
- Function? CheckKoreaChar (str )? {???????
- ?????For(I = 0 ;? I < li><>
- ?????????If(Str. charCodeAt (I)?>? 0x3130? &&? Str. charCodeAt (I )? =? 0xAC00? &&? Str. charCodeAt (I )? <=? 0xD7A3 )))? {???????
- ?????????????Return? True ;???????
- ?????????}???????
- ?????}???????
- ?????Return? False ;???????
- }???????
- ?????
- // Determine whether a Chinese character-GBK exists? (JavaScript )???????
- Function? Check_chinese_char (s ){???????
- ?????Return? (S. length ?! =? S. replace (/[^ \ x00-\ xff]/g, "**"). length );???????
- }???
?
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, 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: Read <精通正则表达式> I 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] * ^ /???, No parentheses are 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 ");
}
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 as a. php file)
$ 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 "the [". $ str. "] you entered contains illegal characters ";??
??? }
??? Else
??? {
??????? Echo "the [". $ str. "] you entered is completely legal. pass! ";??
??? }
}
?>
GBK:
Preg_match ("/^ [". chr (0xa1 ). "-". chr (0xff ). "A-Za-z0-9 _] + $/", $ str); // regular expression of GB2312 Chinese characters, letters, numbers, underscores (_)
?
$ Str = "aaa ";
If (! Eregi ("[^ \ x80-\ xff]", "$ str "))
{
Echo "yes ";
}
Else
{
Echo "not ";
}
?>
$ Str = "China ";
Echo $ str;
Echo "";
// 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 ";
}
?>
To determine whether a string contains Chinese characters, use the following code:
If (preg_match ("/[\ x7f-\ xff]/", $ string )){
Echo "has Chinese characters ";
} Else {
Echo "no Chinese ";
}
?
?
?
?
?