查找分隔符之间的所有匹配项

Find all matches between delimeters

如何替换分隔符之间的所有 <p> 标签?我的文字是

<p>other text...</p>
<p>other text...</p>
``text text...</p>
<p>text text...</p>
<p>text text ...``
<p>other text...</p>
<p>other text...</p>
``text text...</p>
<p>text text...</p>
<p>text text ...``
<p>other text...</p>
<p>other text...</p>
``text text...</p>
<p>text text...</p>
<p>text text ...``
<p>other text...</p>
<p>other text...</p>

我想匹配双反引号之间的所有 <p> 标签(``此处所有 p 标签``)作为分隔符,并将它们替换为空字符串。如果使用正则表达式 /<\/?p>/i 分隔符外没有其他文本,我可以匹配 p 标签,但是如果分隔符外有文本和其他 p 标签,我如何匹配分隔符内的所有 p 标签。

这是 preg_replace_callback 的工作:

$str = <<<EOD
<p>other text...</p>
<p>other text...</p>
``text text...</p>
<p>text text...</p>
<p>text text ...``
<p>other text...</p>
<p>other text...</p>
``text text...</p>
<p>text text...</p>
<p>text text ...``
<p>other text...</p>
<p>other text...</p>
``text text...</p>
<p>text text...</p>
<p>text text ...``
<p>other text...</p>
<p>other text...</p>
EOD;

$res = preg_replace_callback(
        '/``(?:(?!``).)*``/s', 
        function ($m) {
            return preg_replace('~</?p>~', '', $m[0]);
        },
        $str);
echo $res;

输出:

<p>other text...</p>
<p>other text...</p>
``text text...
text text...
text text ...``
<p>other text...</p>
<p>other text...</p>
``text text...
text text...
text text ...``
<p>other text...</p>
<p>other text...</p>
``text text...
text text...
text text ...``
<p>other text...</p>
<p>other text...</p>