首页 > 解决方案 > 如果它包含字符串,如何删除整个句子?PHP

问题描述

我正在尝试通过删除与我没有的其他文本相关的某些短语来使用 PHP 清理大文本。这些短语通常位于两个 html 标记之间,例如<i>this</i>,但我只想删除那些包含关键字的短语,即“另见”。

有没有办法用preg_replace做到这一点?

给定以下输入:

<h1>this is a header</h1>
<i>See also staying safe in Taiwan</i>
<p>Some long text here</p>
<i>Some more text over here</i>
<p>Some more text <i>here</i></p>

如何删除包含字符串“另见”的整个短语。预期输出:

<h1>this is a header</h1>
<p>Some long text here</p>
<i>Some more text over here</i>
<p>Some more text <i>here</i></p>

需要注意的是“另见”。

谢谢!

标签: php

解决方案


这就是我解决它的方法:

$text = preg_replace("/<i>(See also)([^<]*)<\/i>/i",'', $text);

推荐阅读