PHP 去除非汉字字符



$str = "a asdf*&^(())@!2b<中文>。KJHxxyy字符";
preg_match_all('/[\x{4e00}-\x{9fff}]+/u', $str, $matches);
$str = implode('', $matches[0]);

echo $str;


以上只适合utf-8如果要gb2312的先转码,过滤再转码。

$str = mb_convert_encoding($str, 'UTF-8', 'GB2312');

//过滤

$str = mb_convert_encoding($str, 'GB2312', 'UTF-8');

发表评论