2012-02-01 94 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