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
regex - extract last group of numbers in a string
Moderator: General Moderators
-
markjohnson
- Forum Newbie
- Posts: 15
- Joined: Sat Feb 07, 2009 8:36 pm
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
Re: regex - extract last group of numbers in a string
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.
Code: Select all
/[0-9]+/Re: regex - extract last group of numbers in a string
Or just use preg_match with a different expression.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.Code: Select all
/[0-9]+/
Code: Select all
/\d+$/