使用DOMDocument删除HTML标签 [英] Remove HTML Tag using DOMDocument

查看:66
本文介绍了使用DOMDocument删除HTML标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的html中删除< font> 标记,并尝试使用 replaceChild 这样做,但是它似乎无法正常工作.任何人都可以抓住可能出问题的地方吗?

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();

据我了解, $ font_tags 是一个 DOMNodeList ,因此我需要对其进行两次迭代才能使用 DOMNode :: replaceChild 功能.然后,我只想用标签内的内容替换当前值.但是,当我输出$ html时,没有任何变化.有什么想法可能是错的吗?

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?

此处是一个用于测试代码的PHP沙箱.

Here is a PHP Sandbox to test the code.

推荐答案

我将我的评论插入行内

$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();

更新的沙箱:希望我能正确理解您的要求

这篇关于使用DOMDocument删除HTML标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆