Nice job on the scrape code ReidMe
I've rewritten the code so that it can be reusable.
<?php
// Load the entire contents of a url
function LoadUrlContent($url, &$content)
{
$content = "";
$fp = fopen($url, "r");
while (!feof($fp))
{
// chunk in 4K a time
$content .= fgets($fp, 4096);
}
fclose($fp);
}
// Scrape a page based on starting and ending pattern
function PageScrape($url, $strStartPattern, $strEndPattern)
{
$strScrape = "";
// read in the entire $url page into memory
LoadUrlContent($url, $content);
// search for starting text and position to beginning of text to be scraped
$pos = strpos($content, $strStartPattern);
if ($pos == false)
return($strScrape);
$startPos = $pos + strlen($strStartPattern);
// search for ending text
$endPos = strpos($content, $strEndPattern);
if ($endPos == false)
return($strScrape);
// Scrape the text
if ($endPos >= 0 && $startPos >= 0)
{
$len = $endPos - $startPos;
$strScrape = substr($content, $startPos, $len);
}
return($strScrape);
}
Example:
Get Google page information:
echo PageScrape("http://www.google.com", "<font size=-2> - Searching ", " web pages</font>");
Get Yahoo news information:
echo PageScrape("http://www.yahoo.com", "<table width=100% cellpadding=8 cellspacing=0 border=0 bgcolor=f1f1fd class=yhmnwbd>", "<hr noshade size=1 color=d0d3f2>");
P.S.
I hold no responsibility for usage of this code. For educational purposes only. 😃