If I have a string like:
<p> </p>
<p></p>
<p class="a"><br /></p>
<p class="b"> </p>
<p>blah blah blah this is some real content</p>
<p> </p>
<p></p>
<p class="a"><br /></p>
How can I turn it into just:
<p>blah blah blah this is some real content</p>
The regex needs to pick up
s and spaces.
$result = preg_replace('#<p[^>]*>(\s| ?)*</p>#', '', $input);
This doesn't catch literal nbsp characters in the output, but that's very rare to see.
Since you're dealing with HTML, if this is user-input I might suggest using HTML Purifier, which will also deal with XSS vulnerabilities. The configuration setting you want there to remove empty p tags is %AutoFormat.RemoveEmpty.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With