php 如何从字符串中删除空段落标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3809108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:11:40  来源:igfitidea点击:

How to remove empty paragraph tags from string?

php

提问by Ahmad Fouad

I ran into a slight coding problem with WordPress template. This is the code I use in template:

我在使用 WordPress 模板时遇到了一个轻微的编码问题。这是我在模板中使用的代码:

<?php echo teaser(40); ?>

In my functions, I use this to strip tags and get content from allowed tags only.

在我的函数中,我使用它来剥离标签并仅从允许的标签中获取内容。

<?php
function teaser($limit) {
    $content = explode(' ', get_the_content(), $limit);
    if (count($content)>=$limit) {
    array_pop($content);
    $content = implode(" ",$content).'...';
    } else {
    $content = implode(" ",$content);
    }   
    $content = preg_replace('/\[.+\]/','', $content);
    $content = apply_filters('the_content', $content); 
    $content = str_replace(']]>', ']]&gt;', $content);
    $content = strip_tags($content, '<p><a><ul><li><i><em><strong>');
    return $content;
}
?>

The problem: I use the above code to strip tags from the content, but WordPress already puts image tags within paragraph. So the result is empty paragraph tags where images are stripped.

问题:我使用上面的代码从内容中去除标签,但是 WordPress 已经在段落中放置了图像标签。所以结果是空的段落标签,其中图像被剥离。

Just for the sake of cleaning up my code and useless empty tags. My question is how to remove empty paragraph tags?

只是为了清理我的代码和无用的空标签。我的问题是如何删除空段落标签?

<p></p>

Thanks a lot in advance! :)

非常感谢!:)

回答by Pramendra Gupta

use this regex to remove empty paragraph

使用此正则表达式删除空段落

/<p[^>]*><\/p[^>]*>/

example

例子

<?php
$html = "abc<p></p><p>dd</p><b>non-empty</b>"; 
$pattern = "/<p[^>]*><\/p[^>]*>/"; 
//$pattern = "/<[^\/>]*>([\s]?)*<\/[^>]*>/";  use this pattern to remove any empty tag

echo preg_replace($pattern, '', $html); 
// output
//abc<p>dd</p><b>non-empty</b>
?>

回答by blackpla9ue

This gets rid of all empty ptags, even if they contain spacesor &nbps;inside.

这摆脱了所有空p标签,即使它们包含spaces&nbps;在里面。

$str = "<p>  </p><p> &nbsp; </p><p>&nbsp</p><p>Sample Text</p>";

echo preg_replace("/<p[^>]*>(?:\s|&nbsp;)*<\/p>/", '', $str);

This only echoes <p>Sample Text</p>

这只能呼应 <p>Sample Text</p>

回答by Alejandro Salamanca Mazuelo

This function remove empty elements and then, other empty elements, created from previous removed elements. (The example string contains spaces, tabs and carriage returns.)

此函数删除空元素,然后从先前删除的元素创建其他空元素。(示例字符串包含空格、制表符和回车。)

function teaser( $html ) {
    $html = str_replace( '&nbsp;', ' ', $html );
    do {
        $tmp = $html;
        $html = preg_replace(
            '#<([^ >]+)[^>]*>[[:space:]]*</>#', '', $html );
    } while ( $html !== $tmp );

    return $html;
}

Having the following example:

有以下例子:

<?php

    $html = '
    <p>Hello!
        <div class="foo">
            <p id="nobody">
                <span src="ok">&nbsp;</span>
            </p>
        </div>
    </p>
    ';

echo teaser( $html );

?>

The function returns:

该函数返回:

<p>Hello!

</p>

回答by burkestar

Use str_replace to remove empty <p></p>tags.

使用 str_replace 删除空<p></p>标签。

$content = str_replace("<p></p>","",$content);

Anything more advanced will need regular expressions.

任何更高级的东西都需要正则表达式。

回答by Computerish

$content = preg_replace('/<p[^>]*?></p>/', $content);

Update: should be:

更新:应该是:

$content = preg_replace('/<p [^>]*></p>/', $content);

回答by burkestar

Remove the <p>tag from your strip_tags call. The second argument lists the acceptable tags (that don't get stripped).

<p>从 strip_tags 调用中删除标签。第二个参数列出了可接受的标签(不会被剥离)。

$content = strip_tags($content, '<a><ul><li><i><em><strong>');

回答by Alberto Gomez

Have you tried the CSS3 solution?

您是否尝试过 CSS3 解决方案?

p:empty {display: none;}