Home > Backend Development > PHP Problem > PHP determines whether a character is a Chinese character

PHP determines whether a character is a Chinese character

(*-*)浩
Release: 2023-02-27 17:02:02
Original
10511 people have browsed it

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
Copy after login

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

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!

Related labels:
php
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