Home > Backend Development > PHP Tutorial > PHP uses regular expressions to verify Chinese_php tips

PHP uses regular expressions to verify Chinese_php tips

WBOY
Release: 2016-05-16 19:54:48
Original
1087 people have browsed it

php uses preg_match to match and determine whether a string contains Chinese or is all Chinese as follows:

$str = 'php学习博客';
if(preg_match('/[\x7f-\xff]/', $str)){
 echo '字符串中有中文<br/>';
}else{
 echo '字符串中没有中文<br/>';
}

if(preg_match('/^[\x7f-\xff]+$/', $str)){
 echo '字符串全是中文';
}else{
 echo '字符串不全是中文';
}

Copy after login

The output result of the above program is:

There is Chinese in the string
The strings are not all in Chinese

It has been tested under utf-8 and gbk encoding, and both can be used.

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