Page 1 of 1

How to look for a number from 1 to 4 chars long

Posted: Thu Aug 09, 2012 11:50 am
by m2244
Hello,

I am trying to set a variable to a number from a string. The number can be 1 to 4 characters long. Right now I have it returning the index for the start of the number, I need it to return the number itself.

In the example below I want to captrue the '581'. This is in AS3 by the way.

Any suggestions?

Code: Select all

width="581"

Re: How to look for a number from 1 to 4 chars long

Posted: Thu Aug 09, 2012 1:15 pm
by tr0gd0rr
If the string is html, it would be best to parse it with DOMDocument. Otherwise you can use preg_match.

Re: How to look for a number from 1 to 4 chars long

Posted: Thu Aug 09, 2012 5:23 pm
by ragax
A regex option:

Code: Select all

$string = 'width="581"';
$regex = '~(?i)width="(\d{1,4})~';
$hit = preg_match($regex,$string,$match);
if($hit) echo $match[1].'<br />';
Now go read up on regex to understand why that works.
:D