Created
May 16, 2011 11:41
-
-
Save omundy/974299 to your computer and use it in GitHub Desktop.
Basic scraping demo with "foreach" parsing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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