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

WBOY
Release: 2016-06-23 13:12:38
Original
1305 people have browsed it


        $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 '
Copy after login
'; echo "str1: {$str}
";        var_dump($tmp1);        echo '
Copy after login
'; echo "str2: {$str}
";        var_dump($tmp2);        echo '
Copy after login
'; echo "str3: {$str}
";        var_dump($tmp3);        echo '
Copy after login
'; //期望结果: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;
Copy after login
abc1好234OO哈o

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

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

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template