我试图在另一个网站上显示一个网站的内容。这是我目前有:PHP获取文件内容
<?php
$file = file_get_contents ('http://linkto.com/the-page');
echo $file;
?>
它从该页面获取所有内容。但是,我只需要从中获取一张表。我基本上只需要从<table id=arc>
到</table>
的所有内容。
有没有简单的方法来编辑我的代码来获取该表?
我试图在另一个网站上显示一个网站的内容。这是我目前有:PHP获取文件内容
<?php
$file = file_get_contents ('http://linkto.com/the-page');
echo $file;
?>
它从该页面获取所有内容。但是,我只需要从中获取一张表。我基本上只需要从<table id=arc>
到</table>
的所有内容。
有没有简单的方法来编辑我的代码来获取该表?
这里是使用PHP的源DOM解析器,DOMDocument
溶液:
$doc = new DOMDocument;
$doc->loadHTMLFile('http://linkto.com/the-page');
// Get the desired table
$table = $doc->getElementById('arc');
// Print the table's HTML
echo $doc->saveHTML($table);
我同意这是OP想要的,但是要注意的是它仍然加载所有的内容,它只是提供了一种**访问*所需内容的方式。可能无关紧要,但是... – jmoreno
@jmoreno - 我会对不会从远程站点加载所有内容的解决方案感兴趣。 ':)' – nickb
@nickb如果它是HTTP(我假设它是),那么[HTTP Range header](http://www.cyberciti.biz/cloud-computing/http-status-code-206- commad-line-test /)会在资源具有'Accept-ranges:bytes'的情况下工作。并不是说这是一个好主意,因为没有什么要知道'#arc'元素在哪个范围内。 –
开始阅读有关[SimpleXML的](http://us2.php.net/manual/en/book.simplexml.php)和['DOMDocument'](http://us3.php.net/manual/en/class.domdocument.php)两者都很难使用。 –
,当然你有权使用这个其他网站页面。 – 2013-06-23 01:23:08