2010-08-27 140 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

相關問題