Home  >  Article  >  Backend Development  >  Detailed explanation of php Chinese character regular expression examples_PHP tutorial

Detailed explanation of php Chinese character regular expression examples_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:48:34940browse

Some friends may find Chinese character regularization in PHP very simple, but when using it, you will find that there may be some differences between gbk encoding and uft8 encoding. Let me introduce it below.


Chinese character regularity under gbk encoding


1. Determine whether the string is all Chinese characters

Statement:
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
The code is as follows
 代码如下 复制代码
$str = '全部是汉字测试';
if (preg_match_all("/^([x81-xfe][x40-xfe])+$/", $str, $match)) {
echo '全部是汉字';
} else {
echo '不全是汉字';
}
?>
Copy code


$str = 'All are Chinese character tests';

If (preg_match_all("/^([x81-xfe][x40-xfe])+$/", $str, $match)) {

echo 'All are Chinese characters';
代码如下 复制代码
$str = '汉字3测试';
if (preg_match("/([x81-xfe][x40-xfe])/", $str, $match)) {
echo '含有汉字';
} else {
echo '不含有汉字';
}
?>
} else {

echo 'Not all Chinese characters';
}

?>

When $str = 'All are Chinese characters test'; output "All are Chinese characters";

When $str = 'all are Chinese characters test'; output "not all Chinese characters";

 代码如下 复制代码
$str = "php编程";
if (preg_match("/^[x{4e00}-x{9fa5}]+$/u",$str)) {
print("该字符串全部是中文");
} else {
print("该字符串不全部是中文");
}
2. Determine whether the string contains Chinese characters
The code is as follows
Copy code
$str = 'Chinese character 3 test';
If (preg_match("/([x81-xfe][x40-xfe])/", $str, $match)) { } else {            echo 'Does not contain Chinese characters'; } ?> When $str = 'Chinese character 3 test'; output "contains Chinese characters"; When $str = 'abc345'; output "does not contain Chinese characters"; The content of the above variable $str has nothing to do with utf8 or gbk encoding, the judgment result is the same. How to use regular expressions to match Chinese characters under utf-8 encoding
The code is as follows Copy code
$str = "php programming"; if (preg_match("/^[x{4e00}-x{9fa5}]+$/u",$str)) { print("This string is all in Chinese"); } else { print("This string is not all Chinese"); } http://www.bkjia.com/PHPjc/632773.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632773.htmlTechArticleSome friends may find the Chinese character regularization in php very simple, but when using it, you will find that there is a difference between gbk encoding and uft8 encoding There may be some differences, let me introduce them below. Chinese characters encoded in gbk...