The following example program for regular expression judgment (automatically recognizing UTF-8 & amp; gbk encoding) is a regular expression program in which php correctly matches utf8 or gbk Chinese, it can accurately identify Chinese characters in different encoding conditions. & Lt ;? Php $ actiontrim ($ _ get [action]); if ($ actionsub) {& nbsp; & nbs regular expression to judge Chinese characters (UTF-8 & gbk encoding is automatically recognized)
The following example program is a php regular expression program that correctly matches utf8 or gbk Chinese. it can accurately identify Chinese characters in different encoding conditions.
- $ 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) // regular expression of UTF-8 Chinese characters, letters, numbers, underscores (_)
- {
- 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); // gb2312 regular expression of Chinese characters, letters, numbers, underscores (_)