Page 1 of 1
regex - extract last group of numbers in a string
Posted: Sun Jun 21, 2009 3:02 pm
by markjohnson
Hi,
I have:
$string="this-is-my-uri--8271";
How can extract just the last group of numbers at the end, i.e. 8271?
Thanks
Re: regex - extract last group of numbers in a string
Posted: Sun Jun 21, 2009 4:45 pm
by requinix
Try
exploding it on a hyphen and getting the
last element in the returned array.
(Because I think a regular expression would be slower)
Re: regex - extract last group of numbers in a string
Posted: Sun Jun 21, 2009 5:31 pm
by jayshields
Use preg_match() if there's just one group of numbers, if there's more than one group and you want the last group then use preg_match_all() and grab the last element in the array of matches.
Re: regex - extract last group of numbers in a string
Posted: Sun Jun 21, 2009 5:57 pm
by requinix
jayshields wrote:if there's more than one group and you want the last group then use preg_match_all() and grab the last element in the array of matches.
Or just use preg_match with a different expression.