2010-06-16 119 views

回答

31
preg_replace("/[^a-zA-Z0-9\s\p{P}]/", "", $str); 

實施例:

php > echo preg_replace("/[^a-zA-Z0-9\s\p{P}]/", "", "⟺f✆oo☃. ba⟗r!"); 
foo. bar! 

\p{P}匹配所有Unicode標點符號(請參閱Unicode character properties)。如果您只想允許特定的標點符號,只需將它們添加到否定的字符類。 E.g:

preg_replace("/[^a-zA-Z0-9\s.?!]/", "", $str); 
+0

這樣就可以加上:。 ? !正確 – Tedd 2010-06-16 02:35:53

+0

第二個會。第一個允許所有標點符號。 – 2010-06-16 02:36:40

+0

這些似乎去除所有字符:( – Tedd 2010-06-16 02:42:33

3

你將不得不列出標點符號明確,因爲沒有簡寫爲(例如\s是空白字符的簡寫)。

preg_replace('/[^a-zA-Z0-9\s\-=+\|[email protected]#$%^&*()`~\[\]{};:\'",<.>\/?]/', '', $str); 
0
$str = trim($str); 
$str = trim($str, "\x00..\x1F"); 
$str = str_replace(array("&quot;","&#039;","&amp;","&lt;","&gt;"),' ',$str); 
$str = preg_replace('/[^0-9a-zA-Z-]/', ' ', $str); 
$str = preg_replace('/\s\s+/', ' ', $str); 
$str = trim($str); 
$str = preg_replace('/[ ]/', '-', $str); 

希望這有助於。

相關問題