donglu5047 2013-08-29 16:21
浏览 68
已采纳

DOMDocument删除html元素

Here is my code:

$text = '<div class="cgus_post"><a href="?p=15055"><div class="imgbox"><img src="/cgmedia/default.gif"></div></a>
        <h2 id="post-15055">
        <a href="?p=15055" rel="bookmark" title="Permanent Link to Willie Nelson Celebrates 80th Birthday Stoned and Auditioning for Gandalf">Willie Nelson Celebrates 80th Birthday Stoned and Auditioning for Gandalf</a></h2>
        <p>This video pretty much sums up why Willie Nelson is fucking awesome. Willie decided to celebrate his 80th birthday by recording an ‘audition’ for Peter Jackson. &nbsp;Willie wants to take the reigns from Ian McKellan in The Hobbit 2, and decided to show off his acting skills and give some of his own wizardly advice. The result is &nbsp;hilarious. Watch …</p>
        <br class="clear">
        </div>';
$dom = new DomDocument();
$dom->loadHTML($text);
$classname = 'cgus_post';
$finder = new DomXPath($dom);
$nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
foreach($nodes as $node){
    echo $node->nodeValue;  
}

The problem I am having is I am querying for the div that contains the class cgus_post and its returning just the text. How do I have it return the HTML elements also?

  • 写回答

1条回答 默认 最新

  • douchengchu8374 2013-08-29 19:03
    关注

    Here's my innerHTML function that I always use:

    function innerHTML(DOMNode $node, $trim = true, $decode = true) {
       $innerHTML = '';
    
       foreach ($node->childNodes as $inner_node) {
          $temp_container = new DOMDocument();
          $temp_container->appendChild($temp_container->importNode($inner_node, true));
    
          $innerHTML .= ($trim ? trim($temp_container->saveHTML()) : $temp_container->saveHTML());
       }
    
       return ($decode ? html_entity_decode($innerHTML) : $innerHTML);
    }
    

    So then you do:

    $dom = new DOMDocument();
    $dom->loadHTML($html);
    
    echo htmlentities(innerHTML($dom->documentElement->childNodes->item(0)->firstChild));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)