Home > Article > Backend Development > PHP determines whether a character is a Chinese character
This requires the use of regular expressions (Recommended learning: PHP video tutorial)
preg_match('/[\x{4e00}-\x{9fa5}]/u', $str) //UTF-8
This can match the string Whether there is Chinese, if not, return 0
<? $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 "错误输入"; } ?>
Attached, double-byte character encoding range
GBK (GB2312/GB18030)
\x00 -\xff GBK double-byte encoding range
\x20-\x7f ASCII
\xa1-\xff Chinese gb2312
\x80-\xff Chinese gbk
UTF-8 (Unicode)
\u4e00-\u9fa5 (Chinese)
\x3130-\x318F (Korean
\xAC00 -\xD7A3 (Korean)
\u0800-\u4e00 (Japanese)*/
The above is the detailed content of PHP determines whether a character is a Chinese character. For more information, please follow other related articles on the PHP Chinese website!