正则匹配中文汉字根据页面编码不同而略有区别
1.GBK/GB2312编码:[x80-xff]+ 或 [xa1-xff]+
2.UTF-8编码:[x{4e00}-x{9fa5}]+/u
$str = "用户名123";
//把结果放在$chinese中
//GBK/GB2312使用:
preg_match_all("/[\x80-\xff]+/", $str, $chinese);
//UTF-8 使用:
//preg_match_all("/[\x{4e00}-\x{9fa5}]+/u", $str, $chinese);
print_r($chinese);
preg_match("/[\x{4e00}-\x{9fa5}]+/u", $str);//匹配成功返回true,否则false
在使用UTF-8匹配的时候如果出现乱码,可以先把$str转换成utf8格式;
$str= iconv('GBK','UTF-8′,$str);
//或:
//$str= mb_convert_encoding($str,'UTF-8','GBK');
iconv 使用说明
mb_convert_encoding 使用说明
淘宝优惠券领取:http://tao.paetao.com/