doujiu3882
doujiu3882
2014-04-18 08:57

PHP从file_get_contents获取特定标签[重复]

已采纳

This question already has an answer here:

$landings = file_get_contents('http://www.domain.co.uk/page.php');

In the above example URL I want to only select and copy the HTML from a specific element #sidebar-bgbtm

From there I would like to export the HTML as a JSON value.

Is there any way of doing that?

Thanks

</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douxiong3245 douxiong3245 7年前

    If you are familiar with jQuery syntax, PHP Simple HTML DOM might be good place to start http://simplehtmldom.sourceforge.net/

    include('simple_html_dom.php');
    
    $html = file_get_html('http://www.domain.co.uk/page.php');
    
    $result = $html->find('#sidebar-bgbtm', 0)->outertext;
    

    And then export to json:

    echo json_encode( array( 'outertext' => $result) );
    
    点赞 评论 复制链接分享