PHP using regular expressions to match Chinese examples _php tutorial

Source: Internet
Author: User
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 ...

  • Contact Us

    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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.