//Many thanks to Tom Igo, much of this is adapted from his code :)
<?php
/*
AIRNow Web Page Scraper
Language: PHP
*/
// Define variables:
$readParticles = 0; // flag telling you the next line
// is the particle value
$particles = -1; // the particles value
// url of the page with the air quality index data for New York City:
$url =
'http://thomas-gerhardt.com/itp/2007/10/08/flag-waver/';
// open the file at the URL for reading:
$filePath = fopen ($url, "r");
// as long as you haven't reached the end of the file:
while (!feof($filePath))
{
// read one line at a time, and strip all HTML and
// PHP tags from the line:
$line = fgetss($filePath, 4096);
// if the current line contains the substring "AQI observed at"
// then the line following it is either the particle reading
// or the ozone reading:
if (preg_match('/so far/', $line)) {
// if $particles == -1, you haven't gotten
// a value for it yet:
if ($particles == -1) {
$readParticles = 1;
}
// if the previous line was the "observed at line" preceding
// the particle matter reading, then $readParticles = 1 and
// you should get this line, trim everything but the number,
// and save the result in $particles:
if ($readParticles == 1) {
$particles = trim($line);
echo "<$particles>";
$readParticles = 0;
}
}
}
// close the file at the URL, you're done:
fclose($filePath);
?>