The regular expression of PHP Chinese characters

Source: Internet
Author: User

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");
}

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.