【发布时间】:2022-01-22 18:00:32
【问题描述】:
我想检索链接周围的整个句子,由标点符号(例如 . 或 ! 或 ? 或换行符)分隔。
目的是为链接提供更好的上下文。
例如,如果我有这个......
$input = "I don't want this piece! This is the <a href='https://example.com/my-sentence'>sentence</a> I want. In don't want this piece either";
$filter = "https://example.com/my-sentence";
...我需要解决这个问题...
$output = "This is the sentence I want.";
到目前为止,我设法隔离了一个不包含标签的句子,如下所示:
$input = "I don't want this piece. This is the sentence I want. In don't want this piece either";
$filter = "sentence";
$regex = '/[A-Z][^\\.;]*('.$filter.')[^\\.;]*/';
if (preg_match($regex, $input, $match))
$output = $match[0];
这很好用。接下来,我不知道如何绕过url中的标点符号。
我首先探索了隔离锚点并对其进行正则表达式,这适用于任何单个示例,但可能会在野外产生冲突(锚点复制其他锚点或随机文本)。
另一种方法似乎是 strip_tags,类似于...
$input = strip_tags($input);
...问题是我需要同时剥离而不是剥离它们。
也许一个更具体的正则表达式或函数的一些智能包装可以带来一个简单的方法,或者它可能是一个死胡同并且需要一些其他方法,我不知道,但现在我被卡住了,请帮忙!
【问题讨论】:
-
"delimited by punctuation" 可能注定会失败,只要你真正想要的句子会提到
E. A. Milne或@ 987654326@ ... -
确实,我知道这一点,但我希望这种情况很少见。 (我可以指导作者如何放置链接,但如何应用严格的标记......不是真的)