php 如何使用 file_get_contents 或 file_get_html?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14962359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:16:49  来源:igfitidea点击:

How to use file_get_contents or file_get_html?

phpfile-get-contentssimple-html-dom

提问by pguardiario

I've read through quite a few questions on here and I'm not sure if I should be using file_get_contentsor file_get_htmlfor this.

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

All that I'm trying to do is use PHP to display the two tables from this page on my website: http://www.statmyweb.com/recently-analyzed/

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

I know how to get their full page and display it on my site of course, but I can't figure out how I'm able to just pull those two tables without also getting the header/footer.

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

采纳答案by EM-Creations

You are not able to specify in file_get_contents()just to retrieve the tables.

您不能file_get_contents()仅指定 in来检索表。

You would have to get the return value of file_get_contents()using:

您必须获得使用的返回值file_get_contents()

$result = file_get_contents("urlHere");

And then analyse the $resultvariable and extract what information you need to output.

然后对$result变量进行分析,提取出需要输出的信息。

回答by pguardiario

You want file_get_htmlbecause file_get_contentswill load the response body into a string but file_get_htmlwill load it into simple-html-dom.

您想要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];

Alternatively you could use file_get_contentsalong with str_get_html:

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

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

But that would be silly.

但那将是愚蠢的。

回答by programmer

Take the full website content by file_get_contents(), then apply preg_match on the content you have got with <table>and </table>. This will bring you all the contents under the table tags. While showing it in your web page, just put a <table>then echo your matched content and a </table>at the end.

通过 获取完整的网站内容file_get_contents(),然后将 preg_match 应用于您通过<table>和获得的内容</table>。这将为您带来表格标签下的所有内容。在您的网页中显示它时,只需将 a <table>then echo 您的匹配内容和 a</table>放在最后。