Regular expression(regular expression) describes a stringmatching pattern (pattern), which can be used to check whether a string contains a certain substring and match the Substring replacement or removing a substring that meets a certain condition from a certain string, etc.
Regular expressions are text patterns composed of ordinary characters (such as the characters a through z) and special characters (called "metacharacters"). A pattern describes one or more strings to match when searching for text. A regular expression acts as a template that matches a character pattern with a searched string.
This article shares with you the method of using php to verify whether there is Chinese in string. It is very simple and practical. Friends in need can refer to 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 '字符串不全是中文'; }
The output result of the above program is:
There is Chinese in the string
The string is not all Chinese
It has been tested under utf-8 and gbk encoding, and both can be used.
The above is the detailed content of How to use php regular expression to verify Chinese?. For more information, please follow other related articles on the PHP Chinese website!