Home  >  Article  >  Backend Development  >  PHP determines whether a string contains Chinese characters

PHP determines whether a string contains Chinese characters

WBOY
WBOYOriginal
2016-08-08 09:26:33874browse

refer:http://www.wilf.cn/post/php-match-chinese-str.html

Since the advent of 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.

"; //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 "错误输入"; } ?>

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)*/


The above introduces PHP to determine whether a string contains Chinese characters, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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