Home > Backend Development > PHP Tutorial > How to use regex to verify Chinese in PHP

How to use regex to verify Chinese in PHP

墨辰丷
Release: 2023-03-29 16:50:02
Original
3252 people have browsed it

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 &#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:

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!

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