hey, i'm trying to screen scrape a webpage and store certain info in a mysql database so i can search it later.
after i use file_get_contents($url), if i echo contents, the entire webpage (minus images) is shown.
then i've tried using explode to isolate certain strings.
$contents[$i] = explode("<li>", $contents);
$contents[$i] = explode ("/n", $contents);
but when i echo contents i get "A" for $contents[$i] at each $i.
if anybody can help me that would be amazing!!
here is my entire code:
<?php
//require common code (this connects me to database)
require_once("inc/common.inc");
//open HUDS website for reading
$url =
"http://www.huds.harvard.edu/foodpro/center_frame.asp?naFlag=1&sName=HARVARD+UNIVERSITY+$
$handle = fopen($url, "r");
//grab contents from webpage
$contents = file_get_contents($url);
echo "$contents";
fclose($handle);
$empty = "TRUNCATE TABLE `menu2`";
//execute query
$query = mysql_query($empty);
if (!$query)
apologize("Couldn't truncate!");
for($i = 0; $i < strlen($contents); $i ++)
{
$contents[$i] = explode("<li>", $contents);
$contents[$i] = explode ("/n", $contents);
echo "$contents[$i]";
$insert = "INSERT INTO `menu2` (`food`) VALUES ('$contents[i]')";
if(!$insert)
apologize("Could not insert %s into table", $contents[$i]);
//execute query
$result = mysql_query($insert) or die('MySQL error: ' . mysql_error() .
"<hrQuery: $result") ;
if(!$result)
apologize("Error! Check primary key.");
}
//redirect home
redirect("index.php");
?>
the site i am scraping, since i believe the url got cut off is http://www.huds.harvard.edu/foodpro/...a%3E%3Cfont%3E