The regular expression javascript/php of Chinese characters

Source: Internet
Author: User
Tags chr regular expression

This article is a detailed analysis of

"
The code is as follows Copy Code

var reg =/^[ u4e00-u9fa5]+$/;

If (reg.test (str))
{
   alert (' work on Chinese characters ');
}

$action = Trim ($_get[' action '));
if ($action = = "Sub")
{
    $str = $_post[' dir '];  
   //if (!PR Eg_match ("/^[". Chr (0XA1). " -". Chr (0xff)." a-za-z0-9_]+$/", $str))//gb2312 alphanumeric underline regular expression
    if (!preg_match ("/^[x{4e00}-x{9fa5}a-za-z0-9_ ]+$/u ", $str))   //utf-8 Chinese characters alphanumeric underline regular expression
    { 
         echo "<font color=red> you entered [". $str. "] Contains illegal characters </font> "; 
   }
    Else
    {
 & nbsp;      echo "<font color=green> you entered [". $str. "] Completely legal, through!</font> "; 
   }
}


Of course, if you want to match a string full of Chinese characters, the GBK2312 encoding matches:

The code is as follows Copy Code

$str = "Keoko";
if (Preg_match ("/^[". Chr (0XA1). " -". Chr (0xFF)."] +$/", $str)) {
Print ($str. " is really all Chinese characters ");
} else {
Print ($str. " This really TMD is not all Chinese characters ");
}

In fact, as long as we understand the high and low level of each coding start and end, then naturally can write regular, and direct is 16, what is the difficulty? Oh. Note, however, that in PHP, the 16-bit x is used.
So as above, we can also use the regular expression to determine whether the GB2312 Chinese characters

The code is as follows Copy Code

<?php
$str = "Keoko";
if (Preg_match ("/^[xb0-xf7][xa0-xfe]+$/", $str)) {
Print ($str. " is really all Chinese characters ");
} else {
Print ($str. " This really TMD is not all Chinese characters ");
}

?>

In PHP combined with Preg_match ("/^[xb0-xf7][xa0-xfe]+$/", $str) can quickly realize the practice, there is a need for students to refer to.

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.