Remove empty paragraph tags in php.


You can remove empty <p> tags from content using preg_replace function in php. For searching empty paragraph tags in content , we need to use regulars expression code.

See following example.

<?php

$string = '<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p><p></p>'; //content with empty paragraph
$string .= '<p>this paragraph has some content</p>';
$string .= '<p></p>'; //empty paragraph

echo 'string with empty paragraph tags';
echo $string;
echo '<hr/>';

$pattern = "/<p[^>]*><\\/p[^>]*>/"; // regular expression

echo 'string after remove paragraph tags';
echo preg_replace($pattern, '', $string);
?>

See More details about preg_replace() function.
http://php.net/manual/en/function.preg-replace.php

Advertisements

2 thoughts on “Remove empty paragraph tags in php.”

    1. add a space in expression
      $pattern = “/

      ]*> ]*>/”; // regular expression

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s