
LOOK, MA, NO COMPUTER! 131
$readParticles = 0; // flag telling you the next line
// is the particle value
$particles = -1; // the particles value
To extract the data
you need from
those lines, you’ll need a couple more
variables. Add these lines at the top of
the program (but after the line starting
with <?php):
// 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('/AQI observed at /', $line)) {
// if $particles == -1, you haven't gotten
// a value for it yet:
if ($particles == -1) {
$readParticles = 1;
}
}
Then replace the command echo
$line; in the program with this block of
code:
This block uses the preg_match()
command to look for a string of text
matching a pattern you give it. In
this case, it looks for the pattern AQI
observed at. You know that when
you see that line, the next line is the
number you want. When the PHP script
finds the “observed at” line, it sets the
variable $readParticles to 1.
8
// 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 i