Detailed explanation of php Chinese character regular expression examples_PHP tutorial
WBOY
Release: 2016-07-13 10:48:34
Original
976 people have browsed it
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
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)) {
echo 'Contains Chinese characters';
} 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...
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