【发布时间】:2015-01-11 21:36:11
【问题描述】:
我正在尝试创建一个匹配字符串中所有相似单词/短语的模式。
例如,我需要匹配:“this”、“this is”、“this is it”、“that”、“that was”、“that was not”。
它只匹配“this”的第一次出现,但它应该匹配所有次出现。
我什至尝试了锚点和单词边界,但似乎没有任何效果。
我试过(简化):
$content = "this is it! that was not!";
preg_match_all('/(this|this is|this is it|that|that was|that was not)/i', $content, $results);
应该输出什么:
- 这个
- 这是
- 就是这样
- 那个
- 那是
- 那不是
【问题讨论】:
-
使用
/(this( is( it)?)?)/ -
问题:如果您只想捕获您专门搜索的子字符串,为什么不满足于
foreach循环和substr_count? -
@Mr. Llama 你介意发布一个例子吗?
-
@Tom - 作为答案发布:stackoverflow.com/a/26933945/477563
标签: php regex web preg-match preg-match-all