Match Chinese characters in php

Source: Internet
Author: User
Tags regular expression

Example

The code is as follows: Copy code

Echo (mb_eregi ("[x80-xff].", "medium d ")? "Yes": "No"). "Chinese characters ";
Echo (mb_eregi ("^ ([x80-xff].) + $", "Chinese ")? "All Chinese characters": ""); use a function to judge all Chinese characters.

The following uses PHP as an example for matching:

The code is as follows: Copy code

<? Php
$ Str = "learning php is a pleasure. ";
Preg_match_all ("/[x80-xff] +/", $ str, $ match );
// UTF-8 use:
// Preg_match_all ("/[x {4e00}-x {9fa5}] +/u", $ str, $ match );
Print_r ($ match );
?>

Output:

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

)

Regular Chinese characters

The code is as follows: Copy code

$ Str = "how does php eregi match Chinese characters ";
If (preg_match ("/^ [". chr (0x80). "-". chr (0xff). "] + $/", $ str )){
Echo "this is a pure Chinese string ";
} Else {
Echo "this is not a pure Chinese string ";
}
Preg_match_all ($ pat ,......) And preg_replace ($ pat ,......)......


Preg_match_all ("/(Chinese characters) +/ism", "I am a Chinese character, see what you think of me! ", $ M_a );
After the beginning and end of each encoding's high and low levels, you can naturally write regular expressions and directly use sixteen digits. What are the difficulties? Haha. Note that in php, the sixteen bits use x


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

The code is as follows: Copy code

<? Php
$ Str = "example ";
If (preg_match ("/^ [xb0-xf7] [xa0-xfe] + $/", $ str )){
Print ($ str. "all Chinese characters are true ");
} Else {
Print ($ str. "this tc is not all Chinese characters ");
}
?>

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.