2012-02-01 84 views
2

想知道怎樣才能更換我的字符串像所有的特殊字符:hello this is a test!preg_replace函數刪除特殊字符

我已經寫了這個代碼:

$text = preg_replace("/[^A-Za-z0-9]/", ' ', $text); 

這個作品需要更多的靈活性,允許特殊字符像áéíóú...並刪除只有像某些字符::!"#$%&/()=?¿¡ ...

任何想法?

+0

[刪除非Alpha字符正則表達式]的可能重複(http://stackoverflow.com/questions/2197388/deleting-non-alpha-chars-regular-expressions) – 2012-02-01 20:33:26

+0

另請參閱許多其他許多人... http ://stackoverflow.com/search?q = php + preg_replace + alpha – 2012-02-01 20:33:47

+0

定義「特殊」... – 2012-02-01 20:37:36

回答

3

使用$text = preg_replace("/[^\p{L}\p{N}]/u", ' ', $text);

這將匹配不是字母或數字的所有字符,並會適當處理Unicode字母。

+1

+1;如果字符串是UTF-8編碼,則需要與'/ u'修飾符組合。 http://stackoverflow.com/questions/5920188/regexp-greek-chars-by-number/5920342#5920342 – cmbuckley 2012-02-01 23:40:05

+0

謝謝,答案已更新。 – 2012-02-02 14:05:45