我正在建造一个"提及"探测器.提及采取@(user:\d+)
的形式,例如@user:5
.
我还想抓住周围的上下文,让我们在提到之前和之后说最多五个词.我最初 Select 的是
$before = '((?:\S+\s+){0,5})';
$mention = '@(user:\d+)';
$after = '((?:\s+\S+){0,5})';
$pattern = "/{$before}{$mention}{$after}/';
这很有效,除非在之前或之后的组中提到.例如,
Here is some text with @user:123 and @user:456 mentions
应该返回两个匹配项,但preg_match_all
只返回第一个匹配项:
Before | Mention | After |
---|---|---|
Here is some text with |
@user:123 |
and @user:456 mentions |
some text with @user:123 and |
@user:456 |
mentions |
有没有办法一次通过就能做到这一点?
或者,如果我必须找到提及的内容及其位置,例如
preg_match_all('/@(\w+:\d+)/', $text, $mentions, PREG_OFFSET_CAPTURE);
然后再次循环以获得每个提及的上下文,我如何才能在不再次匹配提及的情况下获得"之前"的上下文?