dsztc99732 2015-01-03 20:05
浏览 21
已采纳

循环访问html内容并获取标题和描述 - PHP

I am using str_get_html($string);

The content I am getting is:

> <div class="detail_sec">   <div class="news_post">   
> <h3>Some title</h3>    <p><span class="date_news">2 Jan
> 2015</span></p>    <p align="justify">Read More :: <a
> href="/news/2015/2-jan.pdf" target="_blank">PDF</a> | <a
> href="/news/2015/2-jan.jpg" target="_blank">JPG</a></p>   </div>
> </div>

<div class="detail_sec">
  <div class="news_post">
   <h3>Another title</h3>
   <p><span class="date_news">1 Jan 2015</span></p>
   <p align="justify">Read More :: <a href="/news/2015/1-jan.pdf" target="_blank">PDF</a> | <a href="/news/2015/2-jan.jpg" target="_blank">JPG</a></p>
  </div>
</div>
.
.
.

I want to loop through this content, and get arrays for h3, date_news class, and anchor tags.. I tried this, its working but only h3 (or any other at a time), I want all in single loop.

foreach ( $html->find("div[class=news_post] h3") as $h3) {
    $heading = trim($h3)->plaintext;
    $headingArr[]=$heading;
}
var_dump($headingArr);

Thanks for any help.

  • 写回答

1条回答 默认 最新

  • dtq81142 2015-01-03 20:55
    关注

    You can use a comma as an AND operator when using the selectors, just like you'd do in CSS.

    This for example would return an array of all h3s with div parent AND all spans with div parents:

    $html->find("div h3, div span");
    

    What you are trying to do seems like it should be done client-side in javascript (normally you generate HTML with PHP, you don't process it).

    But there are good reasons to use DOM parsing, so if you are certain that you need to do this in PHP:

    <?php 
    include_once('simple_html_dom.php');
    
    $string = '<div class="detail_sec"><div class="news_post"><h3>Another title</h3><p><span class="date_news">1 Jan 2015</span></p><p align="justify">Read More :: <a href="/news/2015/1-jan.pdf" target="_blank">PDF</a> | <a href="/news/2015/2-jan.jpg" target="_blank">JPG</a></p></div></div>';
    $html=str_get_html($string);
    $headingArr = array();
    
    foreach ( $html->find("div[class=news_post] h3, div[class=news_post] span[class=date_news]") as $h3) {
        $heading = trim($h3->plaintext);
        $headingArr[]=$heading;
    }
    var_dump($headingArr);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?