【发布时间】:2019-06-05 12:16:10
【问题描述】:
我正在尝试使用 preg_replace 从字符串中删除所有标题。它是 wordpress 摘录功能的一部分,删除帖子正文中的所有 <h3>Title</h3> 标签,否则它们会破坏段落流。
所以目标是在诸如
之类的字符串中"<p>Here is some info.</p><h2>A new section</h2><p>Here is some more info.</p>"
我的输出字符串是
"<p>Here is some info.</p><p>Here is some more info.</p>"
我很确定我的正则表达式是正确的。但是 preg_replace 似乎并没有删除标题标签。
$regex = array (
'\<h1[^]]*\>(.*?)\</h1\>',
'\<h2[^]]*\>(.*?)\</h2\>',
'\<h3[^]]*\>(.*?)\</h3\>',
'\<h4[^]]*\>(.*?)\</h4\>',
'\<h5[^]]*\>(.*?)\</h5\>',
'\<h6[^]]*\>(.*?)\</h6\>'
);
preg_replace($regex, '', $text);
标签中的文字牢牢固定在原位,即。
"<p>Here is some info.</p><h2>A new section</h2><p>Here is some more info.</p>"
编辑:没有意识到我的查询缺少分隔符。与其他问题类似,但不会通过谷歌搜索“分隔符”错误找到它们,因为没有收到错误。由于当时无法访问 ftp,因此没有在网站上打开 Php 错误报告。不得不简单地通过 wordpress 主题编辑器进行编辑。可以想象其他人可能有同样的问题,这个问题可能是他们将来的相关参考。
【问题讨论】:
-
也许
preg_replace($regex, '$1', $text);? -
不,那也没用。我很确定我的整个 preg_replace 实际上由于某种原因失败了。当我将其设置为
$text = preg_replace ($regex, '', $text);时,我什么也得不到
标签: php regex wordpress preg-replace