正则表达式删除特定字符

时间:2012-02-05 21:30:45

标签: php regex

我一直在使用以下正则表达式来替换字符串中的所有标点符号:

preg_replace('/[^\w\s]/', '', $tweet);

用\ w作为[a-zA-Z0-9_]的简写,\ s用于省略空格。我在这里学到了这个智慧:Strip punctuation in an address field in PHP。但现在,我需要正则表达式去除除

之外的所有字符
a-z and A-Z
{ and }

所以它应该删除所有的点,逗号,数字等。这是正确的正则表达式是什么?

2 个答案:

答案 0 :(得分:4)

preg_replace('/[^a-zA-Z{} ]/', '', $tweet);

评论中 FakeRainBrigand 提出的更快的变体,谢谢:

preg_replace('/[^a-zA-Z{} ]+/', '', $tweet);

答案 1 :(得分:1)

preg_replace('/[^a-z{}]/i', '', $tweet);