Reputation: 3074
I am using fopen to get a web page's HTML output, aiming for the "Head" part for some information there (meta tags). The problem is that the script takes forever to load the "whole" page then parse only the head part for the needed info.
Is there a way to get a certain length of the page that guarantees a faster processing?
I am using this:
$page = @fopen($_url, "r");
while (!feof($page)) {
$content .= fgets($page, 4096);
}
fclose($page);
Upvotes: 1
Views: 65
Reputation: 86220
You can only get information until you find some specific text. This is the case-sensitive version, which will perform better than insensitive. If you expect your site to always use '</head>'
and not any variations (e.g. </HEAD>
), this will be best.
<?php
// I tested on this page.
$_url = 'http://stackoverflow.com/questions/8466384/get-certain-length-of-a-web-page-to-the-server';
$page = @fopen($_url, "r");
$content = '';
while(!feof($page) && !strpos($content, '</head>')){
$content .= fgets($page, 4096);
}
fclose($page);
echo $content;
?>
To make it insensitive change strpos
to stripos
. The worst case would be never finding the closing tag, which results in the entire page being downloaded -- that's not that bad.
Upvotes: 2