Regular filter string
Chinese encoding contains UTF-8,GBK
$str = "New _ Jian & wen \ piece, clamp abd";
Get results
$res = "new Folder Abd";
Contains illegal characters:
$out [0] = "_";
$out [1] = "&";
$out [2] = "\";
$out [3] = "";
$out [4] = ",";
?>
Regular PHP
Share to:
------Solution--------------------
$str = "New _ Jian & wen \ piece, clamp abd";
echo preg_replace ('/[_&\\\,\s]+/u ', ', $str);
------Solution--------------------
Chinese encoding contains UTF-8,GBK
$str = "New _ Jian & wen \ piece, clamp abd";
Get results
$res = "new Folder Abd";
Contains illegal characters:
$out [0] = "_";
$out [1] = "&";
$out [2] = "\ \";
$out [3] = "";
$out [4] = ",";
$pattern = Join ('
------Solution--------------------
', Array_map (' Preg_quote ', $out));
echo preg_replace ("/$pattern/", "', $str);
New Folder Abd
------Solution--------------------
If the simple English letter is OK range, meet the requirements of [a-za-z0-9], that is, all other than illegal characters.
But if you add a mix of Chinese and English, there is no good way to deduct it unless you are sure of the range of kanji, for example GBK can usually use [\x80-\xff][\x40-\xfe] to denote the range of Chinese characters, note that this is only about the range, and if it is UTF8, it is \u4e00-\ U9fa5, there is no way to kill if there are some characters that you think are illegal.
Roughly speaking, the range of all illegal characters other than Chinese and English letters GBK should be [^a-za-z0-9\x80-\xff\x40-\xfe]
And the UTF8 is [^a-za-z0-9\x{4e00}-\x{9fff}]
------Solution--------------------
echo preg_replace ('/[^a-za-z\p{han}]+/u ', ', ' new _ Jian & wen \ piece, clip abd ');
------Solution--------------------
The GBK on the top of the book is wrong, look at the example of the amount can be known
$test = ' New _ & ' pieces, clamp a ah [set [Abd ';
$matches = Array ();
$reg = "/[^a-za-z0-9\x80-\xff]/";
Preg_match_all ($reg, $test, $matches);
Var_dump ($matches);
$str = ' Set ';
Var_dump (Ord ($str [0]);
Var_dump (Ord ($str [1]);
Var_dump (Ord (' ['));
If it's GBK, you can do it the following way.
$test = ' New _ & ' pieces, clamp a ah [set [Abd ';
$out = Preg_split ('/([a-za-z0-9]
------Solution--------------------
[\x80-\xff].) +/', $test);
Preg_match_all ('/([a-za-z0-9]
------Solution--------------------
[\x80-\xff].) /', $test, $matches);
$res = Implode ($matches [1]);
Var_dump ($res);
$out = Str_split (Str_replace ($matches [1], ", $test));
Var_dump ($out);