php - How do I remove empty <p> elements? -


i have follow regex:

$html = '<p></p><p>lorem ispum...</p><p>  </p><p>;nbsp</p>'; $pattern = "/<p[^>]*><\\/p[^>]*>/"; echo preg_replace($pattern, '', $html ); 

this removes <p> tag if it's empty, i.e. <p></p>. how remove if has other invisible copy in it, such &nbsp;?

there several possible kinds of whitespace , more possibilities "empty" (e.g., <p><em></em></p> empty? or not?).

also consider possibility of having <p class="para"> or <p id="chief">.

much depends on text comes from. microsoft word output &#160;'s in circumstances (i , did unremember them -- sorry).

a reasonable possibility might use regex such #<p>(\\s|&nbsp;)*</p>#mis' match multiple empty lines.

but keep in mind kind of requisite tends rapidly become unreasonable - example class part might force use #<p[^>]*>(\\s|&nbsp;)*</p>#mis' , on - so, might want start looking xml parser instead.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -