PHP regular match string in Chinese _php tutorial

Source: Internet
Author: User
This article summarizes the use of regular expressions in PHP to match some of the example functions of Chinese, the need for friends can refer to.

We need to understand first.
Chinese double Byte character encoding range

1. GBK (gb2312/gb18030)
X00-xff GBK Double byte encoding range
x20-x7f ASCII
Xa1-xff Chinese gb2312
X80-xff Chinese GBK

2. UTF-8 (Unicode)

U4E00-U9FA5 (English)
x3130-x318f (Korean
XAC00-XD7A3 (Korean)
u0800-u4e00 (Japanese)

Gets the specified Chinese and characters
Example 1

The code is as follows Copy Code
$str = ' How do I match Chinese characters in a string? ';
Preg_match ("/Medium/", $STR, $regs);
Print_r ($regs);
?>

Output
Array
(
[0] = in
)

Example 2

The code is as follows Copy Code

$str = ' Here is Chinese this is 中文版 ';
Turn Chinese into red
echo Preg_replace ("/([X80-xff].) /"," 1 ", $str);
Turn non-Chinese into red
echo Preg_replace ("/([x20-x7f])/", "1", $str);
?>


Signed inside the Chinese, English, digital and other mixed, you need to special processing of Chinese characters, because Pcre does not support the U P L and other Perl string processing escape, using 16 binary or Unicode processing, the example is as follows:

The code is as follows Copy Code


$STR = "The two broad of the Gan Jia Kou East # label # 2 #标签3. #标签4, all hair $1234¥XC,CVM, ¥12,DFLKSJF How to # Label 5.x #tag6. CVXCV "";
Preg_match_all ('/# ([a-za-z0-9x7f-xff]+)/', $STR, $mat);
Print_r ($mat);

Preg_match ("/[x{00a5}x{ffe5}" (d+)/U ", $str, $mat);
Print_r ($mat);


Finally, I understand the final correct expression of a regular expression matching Chinese characters in PHP utf-8 encoding--/^[x{4e00}-x{9fa5}]+$/u

The code is as follows Copy Code

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


Instance

The following match Chinese string regular expression is an array, and then many times for the full Chinese character string to print out.
*/

The code is as follows Copy Code

$str _arr = Array (
"Iameverysorry",
"Happy programming, Happy life",
"PHP Tutorial Programming",
"1997 Hong Kong Reunification",
"English Learning abc",
"Www.bkjia.com"
);

$patt _ch = Chr (0XA1). "-" . Chr (0xff);

foreach ($str _arr as $STR)
{
echo "String ' $str ' yes";
if (Preg_match ("/^[$patt _ch]+$/", $str))
{
echo " full Chinese ";
echo "
";
echo "
";
}
Else
{
echo "Non-complete Chinese";
echo "
";
echo "
";
}
}

For more detailed information, please see: http://www.bkjia.com/phper/php-cy/34301.htm


http://www.bkjia.com/PHPjc/444649.html www.bkjia.com true http://www.bkjia.com/PHPjc/444649.html techarticle This article summarizes the use of regular expressions in PHP to match some of the example functions of Chinese, the need for friends can refer to. Let us first understand the Chinese double-byte character encoding range 1. GBK ...

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