Chinese characters in PHP may be some friends feel very simple, but in use will find in the GBK encoding and UFT8 coding may be a little different oh, the following small part to introduce.
Chinese character regularization under GBK coding
1. Determine if the strings are all Chinese characters
Copy CodeThe code is as follows:
$str = ' All are kanji tests ';
if (Preg_match_all ("/^ ([X81-xfe][x40-xfe]) +$/", $str, $match)) {
Echo ' All is Kanji ';
} else {
Echo ' is not all Chinese characters ';
}
?>
When $str = ' All is a kanji test '; When the output is "all Chinese characters";
When $str = ' All is a kanji test '; When the output is "not all Chinese characters";
2. Determine if a string contains Chinese characters
Copy CodeThe code is as follows:
$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 contents of the above variable $str are not related to UTF8 or GBK coding, and the result is the same.
How to match Chinese characters with regular expressions under Utf-8 coding
Copy CodeThe code is as follows:
$STR = "PHP programming";
if (Preg_match ("/^[x{4e00}-x{9fa5}]+$/u", $str)) {
Print ("The string is all Chinese");
} else {
Print ("The string is not all Chinese");
}
http://www.bkjia.com/PHPjc/825101.html www.bkjia.com true http://www.bkjia.com/PHPjc/825101.html techarticle Chinese characters in PHP may be some friends feel very simple, but in use will find in the GBK encoding and UFT8 coding may be a little different oh, the following small part to introduce. GBK coding under the Han ...