1. 程式人生 > >PHP判斷字串中是否含有中文

PHP判斷字串中是否含有中文

<?
$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 "錯誤輸入";
}
?>

另附,雙位元組字元編碼範圍

1. GBK (GB2312/GB18030)
\x00-\xff GBK雙位元組編碼範圍
\x20-\x7f ASCII
\xa1-\xff 中文 gb2312
\x80-\xff 中文 gbk

2. UTF-8 (Unicode)
\u4e00-\u9fa5 (中文)
\x3130-\x318F (韓文
\xAC00-\xD7A3 (韓文)
\u0800-\u4e00 (日文)*/

轉載自:https://www.wilf.cn/post/php-match-chinese-str.html