Page 1 of 1

Expression problem

Posted: Tue Aug 12, 2008 2:36 am
by itsmani1
Hi

I want to fetch 26 out of this using express. I used following code but failed.

Code: Select all

$data = '<span style="text-transform: capitalize;">cloudy</span> 26<abbr title="Temperature in degrees Celsius">°C</abbr>';    
$pattern = '/</span> (.*)<abbr title/msU';
preg_match_all($pattern, $data, $matche1);
thanks

Re: Expression problem

Posted: Tue Aug 12, 2008 2:52 am
by Oren
You failed? of course... you have a syntax error. Don't you see it?

Re: Expression problem

Posted: Tue Aug 12, 2008 2:53 am
by prometheuzz
Since you already used a '/' in your regex, you must can't use '/ ... /' to denote your regex (or escape the '/' but IMO, that looks messy). Also, it;''s safer to use [^<]+ instead of .*.
Here's a way:

Code: Select all

<?php
$data = '<span style="text-transform: capitalize;">cloudy</span> 26<abbr title="Temperature in degrees Celsius">°C</abbr>';    
$pattern = '#</span>\s*+([^<]++)<abbr\s++title#msU';
if(preg_match($pattern, $data, $matches)) {
  echo $matches[1];
}
?>

Re: Expression problem

Posted: Wed Aug 13, 2008 12:15 am
by devendra-m
If I am not wrong. 26(\d+) is the only number in that string

Re: Expression problem

Posted: Wed Aug 13, 2008 5:22 am
by prometheuzz
devendra-m wrote:If I am not wrong. 26(\d+) is the only number in that string
In THAT example there's no other number, but chances are that the OP is handling all kind of input which contain many different numbers since attributes within the tags the OP posted can contain numerical values. It would be like advising to take the sub-string of the characters on index 56 and index 57: it might work on the example input string the OP posted, but it will fail over-all.