Home Backend Development PHP Tutorial php用正则表达式匹配中文实例详解_PHP

php用正则表达式匹配中文实例详解_PHP

Jun 01, 2016 am 11:58 AM
php regular expression

正则表达式

在php中汉字正则可能有些朋友觉得很简单,但是在使用时会发现在gbk编码与uft8编码可能会有点区别哦,下面小编来介绍一下。
gbk编码下汉字正则

1.判断字符串是否全是汉字
复制代码 代码如下:
    $str = '全部是汉字测试';
    if (preg_match_all("/^([x81-xfe][x40-xfe])+$/", $str, $match)) {
        echo '全部是汉字'; 
    } else {
        echo '不全是汉字';
    }
?>

当$str = '全部是汉字测试'; 时输出"全部是汉字";
当$str = 'all全部是汉字测试'; 时输出"不全是汉字";

2.判断字符串是否包含汉字
复制代码 代码如下:
    $str = '汉字3测试';
    if (preg_match("/([x81-xfe][x40-xfe])/", $str, $match)) {
        echo '含有汉字'; 
    } else {
        echo '不含有汉字';
    }
?>

当$str = '汉字3测试'; 时输出"含有汉字";
当$str = 'abc345'; 时输出"不含有汉字";
上述变量$str的内容与utf8还是gbk编码无关,判断结果是一样的。
utf-8编码下用正则表达式如何匹配汉字
复制代码 代码如下:
$str = "php编程";
if (preg_match("/^[x{4e00}-x{9fa5}]+$/u",$str)) {
print("该字符串全部是中文");
} else {
print("该字符串不全部是中文");

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to verify if input is an IPv6 address using PHP regex How to verify if input is an IPv6 address using PHP regex Jun 25, 2023 am 09:37 AM

How to verify if input is an IPv6 address using PHP regex

How to verify if string is empty with PHP regular expression How to verify if string is empty with PHP regular expression Jun 24, 2023 am 08:46 AM

How to verify if string is empty with PHP regular expression

How to verify if it is a file path using regular expression in PHP How to verify if it is a file path using regular expression in PHP Jun 24, 2023 am 10:18 AM

How to verify if it is a file path using regular expression in PHP

PHP regular expression to verify whether the input string is in the format of ID number or passport number PHP regular expression to verify whether the input string is in the format of ID number or passport number Jun 24, 2023 pm 12:11 PM

PHP regular expression to verify whether the input string is in the format of ID number or passport number

How to verify URL address format with PHP regular expression How to verify URL address format with PHP regular expression Jun 24, 2023 am 09:51 AM

How to verify URL address format with PHP regular expression

How to validate phone number format with PHP regular expression How to validate phone number format with PHP regular expression Jun 24, 2023 am 08:44 AM

How to validate phone number format with PHP regular expression

How to use PHP regular expressions to verify whether the input string is the correct ID number, passport number or Hong Kong and Macao pass format How to use PHP regular expressions to verify whether the input string is the correct ID number, passport number or Hong Kong and Macao pass format Jun 24, 2023 am 10:36 AM

How to use PHP regular expressions to verify whether the input string is the correct ID number, passport number or Hong Kong and Macao pass format

How to validate input of specific length using PHP regex How to validate input of specific length using PHP regex Jun 24, 2023 am 10:17 AM

How to validate input of specific length using PHP regex

See all articles