This article shares with you how to use php to verify whether there is Chinese in a string. It is very simple and practical. Friends in need can refer to it
php uses preg_match to match and judge a string The method to determine whether contains Chinese or is all Chinese is 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:
String There are Chinese characters in
, but not all strings are in Chinese characters.
We have conducted some tests under utf-8 and gbk encoding, and both can be used.
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
phpDetailed graphic explanation of string replacement, splitting and connection methods
phpSummary of file system processing methods
phpAchieve DES encryption and decryption examples consistent with c
#
The above is the detailed content of How to use regex to verify Chinese in PHP. For more information, please follow other related articles on the PHP Chinese website!