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:

$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

Very simple 🙂

Leave a Reply

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