Ahmad Fouad
Ahmad Fouad

Reputation: 4107

How to remove empty paragraph tags from string?

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

<?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.

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! :)

Upvotes: 33

Views: 70712

Answers (7)

Malitta N
Malitta N

Reputation: 3423

This gets rid of all empty p tags, even if they contain spaces or &nbps; inside.

$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>

Upvotes: 46

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:]]*</\1>#', '', $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>

Upvotes: 7

Alberto Gomez
Alberto Gomez

Reputation: 29

Have you tried the CSS3 solution?

p:empty {display: none;}

Upvotes: -6

Pramendra Gupta
Pramendra Gupta

Reputation: 14873

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>
?>

Upvotes: 59

Computerish
Computerish

Reputation: 9591

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

Update: should be:

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

Upvotes: 3

burkestar
burkestar

Reputation: 777

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

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

Anything more advanced will need regular expressions.

Upvotes: 4

burkestar
burkestar

Reputation: 777

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

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

Upvotes: -6

Related Questions