[Tfug] Regexps

Stephen Hooper stephen.hooper at gmail.com
Thu May 3 01:11:42 MST 2007


On 5/2/07, Jeremy C. Reed <reed at reedmedia.net> wrote:
> On Wed, 2 May 2007, George Cohn wrote:
>
> > Unfortunately, this gives me a number like 2984 when what I want is two
> > separate numbers, 29 and 84.
>
> You were using ([0-9.]+) so split that at the period and don't put a
> period in the [set]:
>
> ([0-9]+)\.([0-9]+)

if
(preg_match("/<pressure_in>([^\.<]+)(\.([0-9]+))?<\/pressure_in>/i",$weatherPage,$matches))
{
$pressureInt = $matches[1];
$pressureFrac = $matches[3];
}


The only advantage over Jeremy's is that  it will still match if you
are given no fractional part (<pressure in>0</pressure_in>), or a
non-numeric value (such as '<pressure_in>Unavailable</pressure_in>').




More information about the tfug mailing list