php用正则表达式匹配中文实例详解_PHP
Jun 01, 2016 am 11:58 AM正则表达式
在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("该字符串不全部是中文");
}

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

How to verify URL address format with PHP regular expression

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 validate input of specific length using PHP regex
