Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reqular exp to get last number from string

Tags:

regex

php

Hi all
how can i get number(positive num) from string,if string syntax is the following:

t_def_type_id_2
t_def_type_id_22
t_def_type_id_334

so,in the first string i want to get 1,and in the second i want to get 22 and in the third string i want to get 334 using preg_match_all or any other sutable php function

like image 802
ahmad Avatar asked Dec 21 '22 16:12

ahmad


2 Answers

You can use the regex

\d+$

with preg_match

like image 186
codaddict Avatar answered Jan 01 '23 18:01

codaddict


if there is only one number in the string, simply use \d+

like image 39
Daniel Hilgarth Avatar answered Jan 01 '23 17:01

Daniel Hilgarth