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.


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


2 thoughts on “Remove empty paragraph tags in php.

Leave a Reply

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

You are commenting using your 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