Skip to content

Instantly share code, notes, and snippets.

@omundy
Created May 16, 2011 11:41
Show Gist options
  • Save omundy/974299 to your computer and use it in GitHub Desktop.
Save omundy/974299 to your computer and use it in GitHub Desktop.
Basic scraping demo with "foreach" parsing
<?php
/* Basic scraping demo with "foreach" parsing
* Owen Mundy Copyright 2011 GNU/GPL */
$url = "http://www.bbc.co.uk/news/"; // 0. url to start with
$lines = file($url); // 1. get contents of url in an array
foreach ($lines as $line_num => $line) // 2. loop through each line in page
{
// 3. if opening string is found
if(strpos($line, '<h2 class="top-story-header ">'))
{
$get_content = true; // 4. we can start getting content
}
if($get_content == true)
{
$data .= $line . "\n"; // 5. then store content until closing string appears
}
if(strpos($line, "</h2>")) // 6. if closing HTML element found
{
$get_content = false; // 7. stop getting content
}
}
print $data; // 8. print result
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment