Hello,
I want to access a <table> on a remote website and display it on another site. I can access the site with fgets() and read it line by line, but I was hoping to start taking the data starting at the opening <table> tag and end at the closing </table> tag, which would then be dumped into a variable and echoed out.
Any idea what the best way of doing this is?
Cheers,
Jon
u have to use Regular Expressions ( using the function preg_match - php ) or you can use the String functions ( strpos, strstr, substr … etc )
Anyways, use curl…
Then explode HTML with <table>, then explode with </table> and you should have the table (this only works if there is just one table on the website). Something like this:
// you get the HTML with curl here
// and save the return transfer to $html
// I don't have time to write that
$table = explode('<table>', $html);
$table = current(explode('</table>', end($table)));
echo '<table>', $table, '</table>';
// not tested at all so it might not work
It’s ok, I’ve worked it out - probably not the best way but it works.
Here’s the code, should anyone have the same problem in future:
$file_location = "http://www.example.com";
$phrase1 = '<table class="target">'; //the table that I was after had a class, otherwise I'd just use <table>
$phrase2 = '</table>';
$handle = fopen($file_location, 'r');
while(!feof($handle)) {
$line = fgets($handle);
if (stripos($line, $phrase1)) {
$reading = true;
$contents = $phrase1;
}
if (stripos($line, $phrase2)) {
$reading = false;
$contents.= $phrase2;
break;
}
if ($reading) {
$contents.= $line;
}
}
echo $contents;
Cheers,
Jon