Home > Backend Development > PHP Tutorial > Detailed explanation of using regular expressions to match Chinese examples in PHP_PHP Tutorial

Detailed explanation of using regular expressions to match Chinese examples in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:25:25
Original
1469 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

Copy code Code As follows:

$str = 'All are Chinese character tests';
if (preg_match_all("/^([x81-xfe][x40-xfe]) +$/", $str, $match)) {
                                                                                                                                                                                                                                                   🎜>

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

2. Determine whether the string contains Chinese characters



Copy the code
The code is as follows:
$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 characters 3 When testing ';, it outputs "contains Chinese characters";
When $str = 'abc345';, it outputs "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 of.
How to use regular expressions to match Chinese characters under utf-8 encoding



Copy code

The code is as follows:
$str = "php programming"; if (preg_match("/^[x{4e00}-x{9fa5}]+$/u",$str)) { print("The string is all in Chinese" );
} else {
print("This string is not all in Chinese");
}





http://www.bkjia.com/PHPjc/825101.htmlwww.bkjia.com

truehttp: //www.bkjia.com/PHPjc/825101.htmlTechArticleSome friends may find 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. gbk encoding Chinese...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template