[PHP] Filtering All Punctuation with PHP Regular Expressions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// Filter English punctuation
$word = preg_replace("/[[:punct:]]/i", " ", $word);

// Filter Chinese punctuation
mb_regex_encoding('utf-8');
$char = "。、!?:;﹑•"…''""〝〞∕¦‖— 〈〉﹞﹝「」‹›〖〗】【»«』『〕〔》《﹐¸﹕︰﹔!¡?¿﹖﹌﹏﹋'´ˊˋ―﹫︳︴¯_ ̄﹢﹦﹤‐­˜﹟﹩﹠﹪﹡﹨﹍﹉﹎﹊ˇ︵︶︷︸︹︿﹀︺︽︾ˉ﹁﹂﹃﹄︻︼()";
$word = mb_ereg_replace("[".$char."]", " ", $word, "UTF-8");

// Filter consecutive spaces
$word = preg_replace("/\s+/", " ", $word);

mb_ereg_replace is used for Chinese character replacement. When using regex, you don’t need to add / / delimiters.