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: