[Tfug] Regexps - Problem solved

George Cohn gwcohn at simplybits.net
Thu May 3 22:48:15 MST 2007


George Cohn wrote:
> What I am trying to do is parse an xml page from the weather station to 
> pull out the barometric pressure.  This is what I have so far in php:
> 
> #get barometric pressure
> 
> if
> (preg_match("/<pressure_in>([0-9.]+)<\/pressure_in>/i",$weatherPage,$matches))
> {
> $currentPressure=$matches[1];
> }
> 
> This is the string I'm trying to parse:
> 
> <pressure_in>29.84</pressure_in>
> 
> Unfortunately, this gives me a number like 2984 when what I want is two 
> separate numbers, 29 and 84.

Changed preg-match line to this:

(preg_match("/<pressure_in>([0-9]+)\.([0-9]+)<\/pressure_in>/i",$weatherPage,$matches))

Added this for test purposes:

# test code
print (" Match-0: $matches[0]<BR>\n");
print (" Match-1: $matches[1]<BR>\n");
print (" Match-2: $matches[2]<BR>\n");
print (" Match-3: $matches[3]<BR>\n");

This is what I get when the script runs:

  Match-0: <pressure_in>29.84</pressure_in><BR>
  Match-1: 29<BR>
  Match-2: 84<BR>
  Match-3: <BR>

Exactly what I wanted!  Thanks for the tips.

George Cohn




More information about the tfug mailing list