标签:
<? $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 "错误输入"; } ?>
正则匹配中文汉字根据页面编码不同而略有区别: GBK/GB2312编码:[x80-xff]+ 或 [xa1-xff]+ UTF-8编码:[x{4e00}-x{9fa5}]+/u $str = "账单123"; //GBK/GB2312使用: preg_match_all("/[\x80-\xff]+/", $str, $chinese); //UTF-8 使用: //preg_match_all("/[\x{4e00}-\x{9fa5}]+/u", $str, $chinese); print_r($chinese);
标签:
原文地址:http://www.cnblogs.com/phpfensi/p/4563175.html