php - Finding position of last digit in string -


i'm trying format number plates - need find last digit in string , add space after it, e.g.

t4max  = t4 max t53tes = t53 tes 

i'm assuming i'll have use preg_replace - i've tried below find position in string of last digit, returns empty array.

preg_match('/(0-9])/', $plate, $matches, preg_offset_capture); 

any ideas?

that's easy:

$str = 't4max';  $str_with_space = preg_replace('~\d(?=\d*$)~', '$0 ', $str); 

online demo: http://ideone.com/mqqqsh

regex explanation:

~\d(?=\d*$)~ expression means - digit \d followed not digit \d end of string.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -