The regular GBK of Chinese characters under the code
1. Judge whether the string is all Chinese characters
The code is as follows
<?php
$str = ' All is Chinese character test ';
if (Preg_match_all ("/^ ([X81-xfe][x40-xfe]) +$/", $str, $match)) {
Echo ' All is Chinese character ';
} else {
Echo ' is not all Chinese characters ';
}
?>
When $str = ' All is Chinese character test '; When the output "All is Chinese characters";
When $str = ' All is the Chinese character test '; The output is "not all Chinese characters";
2. Determine if the string contains Chinese characters
The code is as follows
<?php
$str = ' Kanji 3 Test ';
if (Preg_match ("/([X81-xfe][x40-xfe])/", $str, $match)) {
Echo ' contains Chinese characters ';
} else {
Echo ' does not contain Chinese characters ';
}
?>
When $str = ' Kanji 3 Test '; When the output "contains Chinese characters";
When $str = ' abc345 '; When the output "does not contain Chinese characters";
The content of the above variable $str is independent of UTF8 or GBK encoding, and the result is the same.
How to match Chinese characters with regular expressions under Utf-8 coding
The code is as follows
$STR = "PHP programming";
if (Preg_match ("/^[x{4e00}-x{9fa5}]+$/u", $str)) {
Print ("The string is all Chinese");
} else {
Print ("This string is not all Chinese");
}