Help with code to increment only numbers (Not Alphabets) in last part of string A10M10N80.
alphanumeric string is not of fixed length, can be A1M2N10
, A10M10N80
, or A102M100N81
, Always number will be in last part.
Separate number from Alphanumeric string store both parts in variable for combining later after incrementing the number.
Ex.alphanumeric string A1M2100N99
, Separate numbers and alphabet from last part,
`A1M2100N99 to A1M2100N and 99`
Increment only numbers(digits) after last alphabet, then combine both parts.
Tried below code, but this code is extracting all numbers and mix all numbers (Result below)
Purpose is to separate numbers then increment digits only combine both parts A1M2100N and 99.
<?php
//$string="A1M01";
$string="A1M2100N99";
$chars = null ;
$nums = null ;
for ($index=0;$index<strlen($string);$index++) {
if(preg_match('/[0-9]/',($string[$index])))
$nums .= $string[$index];
else
$chars .= $string[$index];
}
++$nums;
echo "Letters: $chars n";
echo "Newber : $nums n";
2
Answers
Please check the below solution. After retrieving the number value, you can increment the number.
Match the alpha char(s) but ignore them, then pull the last integer and increment it.