【问题标题】:PHP getting specific tags from file_get_contents [duplicate]PHP从file_get_contents中获取特定标签[重复]
【发布时间】:2014-04-18 08:57:51
【问题描述】:
$landings = file_get_contents('http://www.domain.co.uk/page.php');
在上面的示例 URL 中,我只想从特定元素 #sidebar-bgbtm 中选择和复制 HTML
从那里我想将 HTML 导出为 JSON 值。
有什么办法吗?
谢谢
【问题讨论】:
标签:
php
json
file-get-contents
【解决方案1】:
如果您熟悉 jQuery 语法,PHP Simple HTML DOM 可能是一个不错的起点
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;
然后导出为json:
echo json_encode( array( 'outertext' => $result) );