2010-08-27 81 views
1

我正在寻找一种方法来匹配除某字以外的所有字。匹配除字词外的所有字

请告诉我该怎么写它?

,当我写

$str = "i am a programmer"; 
$word = "am"; 
preg_match_all("/[^($word)]/", $str, $matched);// it do the same, if i when i wrote 
preg_match_all("/[^$word]/", $str, $matched); 

我也尝试preg_match_all("/[^{$word}]/", $str, $matched);但它不会做的工作。

我怎么能告诉除了这个词

感谢很多

+0

类似于:http://stackoverflow.com/questions/242698/regex-to-match-all-words-except-a-given-list – 2010-08-27 14:17:49

+0

你想要结果是什么?一串字母或一个没有这个词的字符串? – 2010-08-27 17:53:59

+0

第一个... – Simon 2010-08-28 07:17:49

回答

1

不能您只需删除该单词的所有实例?

str_replace($word, '', $str); 

或使用explode()或preg_split()拆分?这将为您提供一个由单词分隔的所有部分的数组。

+0

我需要它在大正则表达式中使用它,所以我完全需要写''[^ ...]''因为我需要提及'不是那个单词'。 – Simon 2010-08-27 14:03:38

+0

也许你想环顾一下:http://www.regular-expressions.info/lookaround.html – Sjoerd 2010-08-27 14:04:59

+0

当然,谢谢:) – Simon 2010-08-27 14:09:02

相关问题