How create a increment code to a value like "al003651" in PHP?
Hi i need create a increment code to values like:
al003651 -> al003652 -> al003653
or
m000390 -> m000391 -> m00039开发者_C百科2
note that the number of zeros can vary...
Thanks, Celso.
<?php
$code = 'al003651';
preg_match("~([a-z]+)(\d+)~", $code, $results);
$length = strlen($results[2]);
//add one to the number
$number = (int) $results[2];
$number += 1;
//concat again
$code = $results[1];
//attach zeros
$diff = abs($length - strlen('' . $number));
$code .= str_repeat('0', $diff);
//attach new code
$code .= $number;
echo $code;
?>
PHP has these wonderful incrementors
$str = 'al003651';
for ($i = 0; $i < 16; $i++)
echo $str++,'<br />';
echo '<hr />';
$str = 'm000390';
for ($i = 0; $i < 16; $i++)
echo $str++,'<br />';
Though you'll need to trap for a change of prefix
$str = 'z91';
for ($i = 0; $i < 16; $i++)
echo $str++,'<br />';
When the numeric part has incremented to all 9's, the alpha prefix will change
精彩评论