Aphire Aphire - 1 year ago 84
HTML Question

PHP Scraped content loading at the bottom

Please consider the following code for a web page:

$output = file_get_contents($url);
$start = strpos($output, "<table");
$end = strpos($output, "</table>");
$table = substr($output, $start, ($end - $start));


I would have expected the page to load in this order, however it appears to be loading as:


Table from URL

Now I understand that pulling content from another URL will take longer than simply rendering some '!!!!'s, but as PHP runs server side and I believe runs synchronously I cannot explain why this happens. If someone can explain the situation, and suggest a fix for this, I'd be appreciative.

I am now getting around this by doing some Jquery to reshuffle the order of divs on document ready. It feels very hacky and I don't like it, but for now it'll have to do. If someone can find a better solution, please do let me know.

Answer Source

This happens because the result of the scraped output of the HTML does not include the closing </table> tag.

I can't find any specs describing the following behaviour, but I suspect the browser is unable to determine if the ==== belongs to the table or the body and so has prepend it to the inner HTML of the body.

To fix this, include the length of that closing table tag (8 characters)

$table = substr($output, $start, ($end - $start + strlen('</table>')));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download