PHP matches Chinese character _php tutorial

Source: Internet
Author: User
In PHP if you want the regular to get the characters in the string we need to know the page encoding, the regular match Chinese characters according to the page encoding differs slightly different: gbk/gb2312 encoding: [x80-xff>]+ or [xa1-xff]+] and UTF-8 code: [x{4e00}- X{9fa5}]+/u.

Cases

The code is as follows Copy Code

Echo (Mb_eregi ("[X80-xff].", "Medium D")? "There": "None"). " Chinese characters ";
Echo (Mb_eregi ("^ ([x80-xff].) +$ "," Chinese ")? "All are Chinese characters": ""); Take a look at all Chinese string functions

Here's a sample of PHP to match:

The code is as follows Copy Code

$STR = "Learning php is a happy thing. ";
Preg_match_all ("/[x80-xff]+/", $str, $match);
UTF-8 using:
Preg_match_all ("/[x{4e00}-x{9fa5}]+/u", $str, $match);
Print_r ($match);
?>

Output:

Array
(
[0] = = Array
(
[0] = Learn
[1] = = is a happy thing.
)

)

Regular Chinese characters

The code is as follows Copy Code

$str = "How does eregi in PHP match Chinese characters";
if (Preg_match ("/^[". Chr (0x80). " -". Chr (0xff)." +$/", $str)) {
echo "This is a plain Chinese string";
} else{
echo "This is not a pure Chinese character string";
}
Preg_match_all ($pat,......) With Preg_replace ($pat,......) ......


Preg_match_all ("/(Kanji) +/ism", "I am a kanji, see what you do to me!" ", $m _a);
The beginning and end of the high and low levels of each code, then nature can write the regular, and directly 16-bit, what is the difficulty? Oh. Note, however, that in PHP, it means that 16 bits are X


We can also use this regular expression to determine whether it is a gb2312 character.

The code is as follows Copy Code

$str = "little kid";
if (Preg_match ("/^[xb0-xf7][xa0-xfe]+$/", $str)) {
Print ($str. " Indeed all are Chinese characters ");
} else {
Print ($str. " This true TC is not all Chinese characters ");
}
?>

http://www.bkjia.com/PHPjc/628906.html www.bkjia.com true http://www.bkjia.com/PHPjc/628906.html techarticle in PHP If you want the regular to get the characters in the string we need to know the page encoding, the regular match Chinese characters according to the page encoding different and slightly difference: gbk/gb2312 code: [X80-x ...

  • 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.