php - 如何使用 file_get_contents 或 file_get_html?

标签 php file-get-contents simple-html-dom

我已经阅读了很多关于这里的问题,我不确定我是否应该为此使用 file_get_contentsfile_get_html

我想做的就是使用 PHP 在我的网站上显示此页面中的两个表:http://www.statmyweb.com/recently-analyzed/

当然,我知道如何获取他们的完整页面并将其显示在我的网站上,但我不知道如何在不获取页眉/页脚的情况下只拉出这两个表格。

最佳答案

您需要 file_get_html,因为 file_get_contents 会将响应主体加载到字符串中,而 file_get_html 会将其加载到 simple-html-dom 中。

$dom = file_get_html($url);
$tables = $dom->find('table');
echo $tables[0];
echo $tables[1];

或者,您可以将 file_get_contentsstr_get_html 一起使用:

$dom = str_get_html(file_get_contents($url));

但这很愚蠢。

关于php - 如何使用 file_get_contents 或 file_get_html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14962359/

相关文章:

php - 将代理与 PHP 简单 HTML DOM 解析器结合使用

javascript - 将 Javascript 变量传递给 PHP

php - 更改下拉菜单宽度

php - file_get_contents https 超时 1 分钟?

php POSTstream_context_create 和 file_get_contents

php - 在评论 block 之间获取 html 简单的 HTML DOM

php - 没有任何键的多维数组值打印

php - assert() 需要 1 个参数,给定 2 个

php - 使用file_get_contents()PHP函数的正确方法

php - 获取元素外部的文本