$test="abc123"; //$test="abc123n"; $r = preg_match_all("/.*?(\d+)$/", $test, $matches); //echo $r; //print_r($matches); if($r>0) { echo $matches[count($matches)-1][0]; }
regex is explained as follows:
. *? - it will take all the characters in the string from the beginning until a match is found for the next part.
(\ d +) $ - this is one or more digits to the end of the line, grouped.
without? in the first part, only the second digit will correspond in the second part, because all the numbers in front of it will be occupied. *
Kinjal dixit
source share