...

View Full Version : Regular Expression - Finding numbers.



Apothem
08-01-2008, 02:23 AM
preg_match('/\[Changeset:[0-9]+\]/', '[Changeset:5048]', $matches, PREG_OFFSET_CAPTURE);

print_r($matches);


What I'm trying to do is get the number 5048, but I can't see to get it.

Inigoesdr
08-01-2008, 03:17 AM
You need parenthesis to capture the subpattern you want to match:

preg_match('/\[Changeset:([0-9]+)\]/', '[Changeset:5048]', $matches, PREG_OFFSET_CAPTURE);

print_r($matches);

//Array ( [0] => Array ( [0] => [Changeset:5048] [1] => 0 ) [1] => Array ( [0] => 5048 [1] => 11 ) )

Apothem
08-01-2008, 05:30 AM
Thanks; that did the trick.

Also, is there a way to add one to the integer found? Without the use of using a PHP function, I mean.

Something like:

return preg_replace("/Changesets:([0-9+1]+)-([0-9]+)/", "Changesets $1 to $2", "Changesets:5048-5500");



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum