Home  >  Article  >  Backend Development  >  正则:过滤除英文和汉字的其它特殊符号

正则:过滤除英文和汉字的其它特殊符号

WBOY
WBOYOriginal
2016-06-23 13:12:381255browse


        $str = 'abc…1…好……(...234*&(*&))(  ※  O(∩_∩)O哈! √ (⊙o⊙)? ';        //过滤除英文和汉字的其它特殊符号        $tmp = str_replace($replaceArr, '', $str);        $tmp1 = preg_replace('/(?![a-zA-Z\\x{4e00}-\\x{9fa5}])/', '', $str);    //这个是实际要求        $tmp2 = preg_replace('/([\\x80-\\xff])/', '', $str);    //这个是过滤汉字        $tmp3 = preg_replace('/[\\x{4e00}-\\x{9fa5}]/u', '', $str);    //这个也是过滤汉字        echo "str: {$str} <pre class="brush:php;toolbar:false">";        var_dump($tmp);        echo '
';        echo "str1: {$str} 

";        var_dump($tmp1);        echo '
';        echo "str2: {$str} 
";        var_dump($tmp2);        echo '
';        echo "str3: {$str} 
";        var_dump($tmp3);        echo '
';        //期望结果:abc好       
//当前代码执行结果(全错的)

请教各位正则大神,或者有其它更好的方式。
主要目的:将字符串中的非英文字母和非汉字的其它符号替换为空。默认编码:GB2312


回复讨论(解决方案)

$str = 'abc…1…好……(...234*&(*&))(  ※  O(∩_∩)O哈! √ (⊙o⊙)? ';$str = iconv('gbk', 'utf-8', $str);$str = preg_replace('/[\W_]/u', '', $str);$str = iconv('utf-8', 'gbk', $str);echo $str;
abc1好234OO哈o

像搜狗表情中的特殊符号,还是没法过滤掉么?

那个看来是字母,我搞错了!
感谢xu大大的支持!

版主,我刚才忽略了一个问题,我按照你的代码运行的结果,是没有汉字的:

Statement:
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