Summary of the regular expressions of Php/js Chinese characters _php tutorial

Source: Internet
Author: User
If we want to match the Chinese characters in PHP and JS as long as the use of regular/^[\\x{4e00}-\\x{9fa5}]+$/u, matching double-byte characters (including Chinese characters): [^/x00-/xff] can, specifically as follows

JS version

Regular expressions that match Chinese characters: [/U4E00-/U9FA5]

Match double-byte characters (including kanji): [^/x00-/xff]

The code is as follows Copy Code


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

if (Reg.test (str))
{
Alert (' Work of Chinese characters ');
}

Calculates the length of a string (a double-byte character length meter 2,ascii character 1)

String.prototype.len=function () {return This.replace ([^/x00-/xff]/g, "AA"). Length;

PHP version

PHP matches Chinese characters!
/^[x{4e00}-x{9fa5}]+$/u

The code is as follows Copy Code

$action = Trim ($_get[' action ');
if ($action = = "Sub")
{
$str = $_post[' dir '];
if (!preg_match ("/^[". Chr (0XA1). " -". Chr (0xff)." a-za-z0-9_]+$/", $str))//gb2312 Chinese character alphanumeric underline regular expression
if (!preg_match ("/^[x{4e00}-x{9fa5}a-za-z0-9_]+$/u", $str))//utf-8 Chinese alphanumeric underscore regular expression
{
echo "You entered [". $str. "] contain illegal characters ";
}
Else
{
echo "You entered [". $str. "] Perfectly legal, through! ";
}
}

Of course, if you want the string is all Chinese characters GBK2312 encoding matching is:

The code is as follows Copy Code


$str = "little kid";
if (Preg_match ("/^[". Chr (0XA1). " -". Chr (0xff)." +$/", $str)) {
Print ($str. " Indeed all are Chinese characters ");
} else {
Print ($str. " This really is not all a TMD is Chinese characters ");
}

UFT8 coded Regular

$str = "Kanji";
if (Preg_match ("/^[x{4e00}-x{9fa5}]+$/u", $str)) {
Print ("The string is all Chinese");
} else {
Print ("The string is not all Chinese");
}


In fact, as long as the understanding of the high and low levels of each code start and end, then nature can write regular, and directly 16-bit, what is the difficulty? Oh. Note, however, that in PHP, the 16-bit is the x that is used.

Examples of gbk,gb2312:

The code is as follows Copy Code

$action = Trim ($_get[' action ');
if ($action = = "Sub")
{
$str = $_post[' dir '];
if (!preg_match ("/^[". Chr (0XA1). " -". Chr (0xff)." a-za-z0-9_]+$/", $str))//gb2312 Chinese character alphanumeric underline regular expression
if (!preg_match ("/^[x{4e00}-x{9fa5}a-za-z0-9_]+$/u", $str))//utf-8 Chinese alphanumeric underscore regular expression
{
echo "You entered [". $str. "] contain illegal characters ";
}
Else
{
echo "You entered [". $str. "] Perfectly legal, through! ";
}
}
?>

The meaning of +$/u:

+ means repeat 1 or more times;
$ represents the end of the match;
/denotes delimiters;
U indicates that the pattern string is treated as UTF-8;
U means stop searching after the first match.

To match 2-4, denoted by {2,4}.
/^[x{4e00}-x{9fa5}]{2,4}$/u

http://www.bkjia.com/PHPjc/631567.html www.bkjia.com true http://www.bkjia.com/PHPjc/631567.html techarticle if we want to match the Chinese characters in PHP and JS as long as the use of regular/^[\\x{4e00}-\\x{9fa5}]+$/u, matching double-byte characters (including Chinese characters): [^/x00-/xff], the following JS version of the match ...

  • Related Article

    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.