How to use file_get_contents or file_get_html?

I’ve read through quite a few questions on here and I’m not sure if I should be using file_get_contents or file_get_html for this.

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/

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.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

You want file_get_html because file_get_contents will load the response body into a string but file_get_html will load it into simple-html-dom.

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

Alternatively you could use file_get_contents along with str_get_html:

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

But that would be silly.

Solution 2

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

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

$result = file_get_contents("urlHere");

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

Solution 3

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.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply