Note that there are some explanatory texts on larger screens.

plurals
  1. PORedundant tags to be removed
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/10248166/cleaning-html-by-removing-extra-redundant-formatting-tags">Cleaning HTML by removing extra/redundant formatting tags</a> </p> </blockquote> <p>I have been trying to remove redundant tags which are generated from HTML composers. This apparently is not able to remove all the empty ones. I have been looking at it for sometime and I am not able to figure out. There might be something I am missing. </p> <p>Below is the code. Thanks a lot ppl.. </p> <pre><code>//Check for reduntant tags function removeRedundantTags($pathname) { $dom = new DOMDocument(); $dom-&gt;loadHTMLFile($pathname); $allTags = $dom-&gt;getElementsByTagName('*'); for($i = 0; $i &lt; $allTags-&gt;length; $i++) { $currentTag = $allTags-&gt;item($i); echo "Accessed Tags: ".$currentTag-&gt;nodeName.'&lt;br&gt;'; if($currentTag-&gt;hasChildNodes()) continue; if($currentTag-&gt;nodeName == 'br' || $currentTag-&gt;nodeName == 'img' || $currentTag-&gt;nodeName == 'meta') continue; if($currentTag-&gt;nodeValue == NULL) { $parentNode = $currentTag-&gt;parentNode; $oldChild = $parentNode-&gt;removeChild($currentTag); echo "Removed Tags----: ".$oldChild-&gt;nodeName.'&lt;br&gt;'; } } echo "Redandant Removed&lt;br&gt;"; $dom-&gt;saveHTMLFile($pathname); } </code></pre> <p><strong>Edit</strong> (output added) Lets saying I am trying to cleanup span tags (sorry I am not able to post HTML code) It is just removing half of it.. It is like two of the span tags are present it removes only one, and the same applies to all of the empty tags</p> <p>I am using DOM structure which happens to be very fast as I will be using this piece of code to hundreds of HTML files. So some of the answers use regular expressions which are not helpful. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload