dongyue3795 2014-10-30 05:46
浏览 132
已采纳

通过DOMDocument PHP获取DIV元素内容

I have to recover some news from a div of a site. The div is structured as follows:

The HTML Markup:

<ul id="news-accordion" class="rounded" style="padding: 2px;">
   <li class="o">
         <h3>
            <span>TITLE ARTICLE</span>
            <span>30/10/2014</span>
         </h3>
         <div style="display: none;">
              <p>text of article</p>
         </div>
   </li>
   <li class="e">
         <h3>
            <span>TITLE ARTICLE</span>
            <span>28/10/2014</span>
         </h3>
         <div style="display: none;">
              <p>text of article</p>
         </div>
   </li>
   <li class="o">
         <h3>
            <span>TITLE ARTICLE</span>
            <span>29/10/2014</span>
         </h3>
         <div style="display: none;">
              <p>text of article</p>
         </div>
   </li>                                                     
</ul>

PHP

<?php 

$doc = new DomDocument;
$doc->validateOnParse = true;
$doc->loadHtml(file_get_contents('http://www.xxxxxxxxx/news.php'));

$news = $doc->getElementById('news-accordion');

$li = $news->getElementsByTagName('li'); 

foreach ($li as $row){ 

    $title = $row->getElementsByTagName('h3'); 
    echo $title->item(0)->nodeValue."<br><br>"; 

    /*foreach ($title as $row2){ 
    echo $row2->nodeValue."<br><br>";
    //echo $row2->item(0)->nodeValue."<br><br>"; 
    }*/

    $text = $row->getElementsByTagName('p'); 
    echo utf8_decode($text->item(0)->nodeValue)."<br><br><br>"; 

}

?>

The code works correctly, but when I print the contents of the span tag echo $title->item(0)->nodeValue;,

The text of the two span is printed together.

How can I take the contents of the two span separately? Thanks.

展开全部

  • 写回答

2条回答 默认 最新

  • dongxu4580 2014-10-30 05:59
    关注

    Yes you can, just adjust the ->item() index. Just like what you have done already in the other elements, point it to that header element, then just explicitly point it to those span children:

    foreach ($li as $row){ 
    
        $h3 = $row->getElementsByTagName('h3')->item(0);
        $title = $h3->getElementsByTagName('span')->item(0); // first span
        $date = $h3->getElementsByTagName('span')->item(1); // second span
    
        echo $title->nodeValue . '<br/>';
        echo $date->nodeValue . '<br/>';
    
        $text = $row->getElementsByTagName('p'); 
        echo utf8_decode($text->item(0)->nodeValue)."<br><br><br>"; 
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部