Home >Backend Development >PHP Problem >How to determine whether a string contains Chinese characters in PHP
In PHP, you can use the preg_match() function with regular expressions to determine whether the string contains Chinese characters. The syntax format is "preg_match("/[\x7f-\xff]/", string) "; If 1 is returned, it contains Chinese, if 0 is returned, it does not contain Chinese.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Since there is black hat SEO, Dealing with SPAM (spam messages) has always been one of the tasks of website staff. What persecutes us is either spam message mass-sending machines or spam user registration machines.
Determining whether a string contains Chinese characters is one of the ways to deal with SPAM. It can effectively prevent pure English spam messages and can also be used to regulate user registration. Look at the code below, it is compatible with gb2312 and utf-8.
<?php header('content-type:text/html;charset=utf-8'); $str = "测试中文"; echo $str; echo "<hr>"; //if (preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/", $str)) { //只能在GB2312情况下使用 //if (preg_match("/^[\x7f-\xff]+$/", $str)) { //兼容gb2312,utf-8 //判断字符串是否全是中文 if (preg_match("/[\x7f-\xff]/", $str)) { //判断字符串中是否有中文 echo "含有中文,正确输入"; } else { echo "不含中文,错误输入"; } ?>
Output:
Attached, double-byte character encoding range
1. GBK (GB2312 /GB18030)
\x00-\xff GBK double-byte encoding range
\x20-\x7f ASCII
\xa1-\xff Chinese gb2312
\x80-\xff Chinese gbk
2. UTF-8 (Unicode)
\u4e00-\u9fa5 (Chinese)
\x3130-\x318F (Korean
\xAC00-\xD7A3 (Korean)
\u0800-\u4e00 (Japanese)*/
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to determine whether a string contains Chinese characters in PHP. For more information, please follow other related articles on the PHP Chinese website!