Home >Backend Development >PHP Tutorial >PHP uses regular expressions to verify Chinese_php tips
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.