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 ?
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  's in circumstances (i , did unremember them -- sorry).
a reasonable possibility might use regex such #<p>(\\s| )*</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| )*</p>#mis' , on - so, might want start looking xml parser instead.
Comments
Post a Comment