Simple_html_dom只获取图片+网址

时间:2015-12-02 10:23:10

标签: php html

我正在尝试使用 simple_html_dom 来获取图片网址和每个帖子的href,这是我迄今为止所能解决的问题。

include('simple_html_dom.php');
foreach ($html->find('div[class="media-body"]') as $div) {
echo $div . '<div style="width: 100%; height: 0px; color: #eee;"></div>';
}; // Something.

如果你向上看,这会拉动我的整个media-body课,就像我想的那样,我试图达到它的最终结果,只拉动下面显示的内容。

<a class="pull-left" href="http://www.example.com">
   <img width="64" height="64" src="http://placehold.it/64x64" class="media-object">
</a>

这是我当前media-body课程内的内容,其中包含我想要获取的网址和图片。

<div class="media-body">
    <div class="media">
      <a class="pull-left" href="http://www.example.com">
       <img width="64" height="64" src="http://placehold.it/64x64" class="media-object">
      </a>
    <i>
       <a class="media-heading" href="http://www.example.com">
       Example Artist - Example Song Name.
       </a>
    </i>    
      <a href="http://www.example.com">
      <button type="button" class="btn btn-success btn-xs">Download</button>
      </a>
    <br>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

<?php 
    include('simple_html_dom.php');

    $html = '<div class="media-body">
        <div class="media">
          <a class="pull-left" href="http://www.example.com">
           <img width="64" height="64" src="http://placehold.it/64x64" class="media-object">
          </a>
        <i>
           <a class="media-heading" href="http://www.example.com">
           Example Artist - Example Song Name.
           </a>
        </i>    
          <a href="http://www.example.com">
          <button type="button" class="btn btn-success btn-xs">Download</button>
          </a>
        <br>
        </div>
    </div>';

    $html = str_get_html($html);
    foreach ($html->find('div[class="media-body"] a[class="pull-left"]') as $div) {
       echo $div . '<div style="width: 100%; height: 0px; color: #eee;"></div>';
    };

?>
相关问题