我试图在另一个网站上显示一个网站的内容。这就是我目前所拥有的:
<?php
$file = file_get_contents ('http://linkto.com/the-page');
echo $file;
?>
它从该页面获取所有内容。但是,我只需要从中获取一个表。我基本上只需要<table id=arc>
到</table>
的所有内容。
有没有简单的方法来编辑我的代码才能获得该表?
答案 0 :(得分:5)
这是使用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);