How remove empty paragraph with php?

Today I will show simple, but simetimes very usefull example, how to remove empty paragrapth from your text.

My code:

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

Very simple 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *