Regular expression: filters out other special characters except English and Chinese characters.

Source: Internet
Author: User
Regular expression: filters out other special characters except English and Chinese characters.
$ Str = 'ABC... 1... Good ...... (... 234 * & (* &) (※o (∩ _ ∩) O ha! √ (⊙ O ⊙ )? '; // Filter other special symbols except English and Chinese characters $ tmp = str_replace ($ replaceArr, '', $ str); $ tmp1 = preg_replace ('/(?! [A-zA-Z \ x {4e00}-\ x {9fa5}])/', '', $ str ); // This is the actual requirement $ tmp2 = preg_replace ('/([\ x80-\ xff])/', '', $ str ); // This is to filter Chinese characters $ tmp3 = preg_replace ('/[\ x {4e00}-\ x {9fa5}]/U', '', $ str ); // This is also used to filter Chinese characters echo "str :{$ str}
";        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 '
'; // Expected result: good abc

// Current code execution result (all wrong)

Please refer to the regular expressions or other better methods.
Purpose: to empty non-English letters and other Chinese characters in a string. Default encoding: GB2312


Reply to discussion (solution)

$ Str = 'ABC... 1... Good ...... (... 234 * & (* &) (※o (∩ _ ∩) O ha! √ (⊙ O ⊙ )? '; $ Str = iconv ('gbk', 'utf-8', $ str); $ str = preg_replace ('/[\ W _]/U ','', $ str); $ str = iconv ('utf-8', 'gbk', $ str); echo $ str;
Abc1 better 234OO Haro

Can't I filter out special characters like Sogou expressions?

It looks like a letter. I made a mistake!
Thank you for your support!

Moderator, I ignored a problem just now. according to the result of running your code, there are no Chinese characters:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.