Home > Backend Development > PHP Tutorial > How to use php regular expression to verify Chinese?

How to use php regular expression to verify Chinese?

怪我咯
Release: 2023-03-13 17:14:02
Original
3036 people have browsed it

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 &#39;字符串中有中文<br/>&#39;;
}else{
 echo &#39;字符串中没有中文<br/>&#39;;
}

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

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!

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