Search code examples
phphtmldomdocument

Remove HTML Tag using DOMDocument


I'd like to remove <font> tags from my html and am trying to use replaceChild to do so, but it doesn't seem to work properly. Can anyone catch what might be wrong?

$html = '<html><body><br><font class="heading2">Limited Size and Resources</font><p><br><strong>Q: When can a member use the limited size and resources exception?</strong></p></body></html>';

$dom = new DOMDocument();
$dom->loadHTML($html);
$font_tags = $dom->GetElementsByTagName('font');

foreach($font_tags as $font_tag) {
  foreach($font_tag as $child) {
    $child->replaceChild($child->nodeValue, $font_tag);
  }
}

echo $dom->saveHTML();

From what I understand, $font_tags is a DOMNodeList, so I need to iterate through it twice in order to use the DOMNode::replaceChild function. I then want to replace the current value with just the content inside of the tags. However, when I output the $html nothing changes. Any ideas what could be wrong?

Here is a PHP Sandbox to test the code.


Solution

  • I'll put my remarks inline

    $html = '<html><body><br><font class="heading2">Limited Size and Resources</font><p><br><strong>Q: When can a member use the limited size and resources exception?</strong></p></body></html>';
    
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    $font_tags = $dom->GetElementsByTagName('font');
    
    /* You only need one loop, as it is iterating your collection 
       You would only need a second loop if each font tag had children of their own
    */
    foreach($font_tags as $font_tag) {
      /* replaceChild replaces children of the node being called
         So, to replace the font tag, call the function on its parent
         $prent will be that reference
      */
      $prent = $font_tag->parentNode;
       /* You can't insert arbitrary text, you have to create a textNode
          That textNode must also be a member of your document
       */
      $prent->replaceChild($dom->createTextNode($font_tag->nodeValue), $font_tag);
    
    }
    
    echo $dom->saveHTML();
    

    Updated Sandbox: Hopefully I understood your requirements correctly