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

PHP uses regular expressions to verify Chinese_php tips

WBOY
WBOYOriginal
2016-05-16 19:54:481086browse

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 '字符串不全是中文';
}

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.

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