Page 1 of 1

Simple Regex question

Posted: Thu Feb 19, 2009 5:31 pm
by ChrisF79
I'm really hoping someone can help here.

How would I search $string below and store 208046194 in a variable that I can work with?

$string = '<td width=85 align=left valign=top class='label'>ML#:</td><td width=175 align=left valign=top>208046194</td>';

Thanks so much in advance for the help!

Re: Simple Regex question

Posted: Thu Feb 19, 2009 11:58 pm
by Benjamin

Code: Select all

 
if (preg_match('#>\s{0,3}(\d{3,})\s{0,3}<#', $string, $match)) {
    echo $match[1];
}
 
This assumes that the number will always be 3 or more digits long. You can change the {3,} to more or less.

Re: Simple Regex question

Posted: Fri Feb 20, 2009 1:10 am
by prometheuzz
astions wrote:

Code: Select all

 
if (preg_match('#>\s{0,3}(\d{3,})\s{0,3}<#', $string, $match)) {
    echo $match[1];
}
 
This assumes that the number will always be 3 or more digits long. You can change the {3,} to more or less.
Why the "\s{0,3}"?

Edit: nvm, I didn't see the '>' before and '<' after these "\s{0,3}". Still, I'd opt for "\s*" instead.

Re: Simple Regex question

Posted: Fri Feb 20, 2009 1:11 am
by Benjamin
The regex doesn't assume there will never be spaces on either side of the digits.

Re: Simple Regex question

Posted: Fri Feb 20, 2009 1:13 am
by prometheuzz
astions wrote:The regex doesn't assume there will never be spaces on either side of the digits.
Wow, your'e fast!
; )