Php 如何去除包裹<;文章>-标签?

Php 如何去除包裹<;文章>-标签?,php,regex,wordpress,Php,Regex,Wordpress,我想删除代码中包装的所有空文章标记,例如: <article> </article> 但它也应该适用于: <article></article> 及 我现在有这个(在我的WordPress主题的function.php中),但它不起作用: function remove_empty_articles($content) { $content = preg_replace('#<article>(.+)<

我想删除代码中包装的所有空文章标记,例如:

 <article>
 </article>

但它也应该适用于:

 <article></article>


我现在有这个(在我的WordPress主题的function.php中),但它不起作用:

function remove_empty_articles($content) {
    $content = preg_replace('#<article>(.+)</article>#', '', $content);
    return $content;
}
add_filter('the_content', 'remove_empty_articles');
function remove\u empty\u articles($content){
$content=preg#u replace('.+)#','.$content);
返回$content;
}
添加过滤器(“内容”,“删除空文章”);

如何解决此问题?

请改用此模式:

#<article>\s*</article>#
\s*#
使用
\s*
表示零个或多个白色字符。不需要捕获组

由于两个原因,您使用的模式是错误的:

  • 匹配除换行符以外的任何字符(这将删除所有单行文章标记,而不会找到多行文章标记。)
  • +量词表示1次或多次,您可以使用它查找

很好,明白了!5分钟结束后,我会马上检查!
#<article>\s*</article>#