PHP获取文件内容

时间:2013-06-23 01:08:04

标签: php file-get-contents

我试图在另一个网站上显示一个网站的内容。这就是我目前所拥有的:

<?php 
  $file = file_get_contents ('http://linkto.com/the-page'); 
  echo $file; 
?>

它从该页面获取所有内容。但是,我只需要从中获取一个表。我基本上只需要<table id=arc></table>的所有内容。

有没有简单的方法来编辑我的代码才能获得该表?

1 个答案:

答案 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);