我想构建一个php脚本,通过将前一个id增加1来自动生成一个新的id。例如:A0009变成A0010,A9999变成B0000
我写了一个有效的,但它的长度不超过 5 个字符:例如:Z9999 应该转到 A00000 等等。
有什么建议么?这是我的片段:
<?php
function replaceChar($string2replace)
{
$charLength = strlen($string2replace)-1;
$charAt = array();
$charAt[4] = substr($string2replace, -1);
$charAt[3] = substr($string2replace, -2,1);
$charAt[2] = substr($string2replace, -3,1);
$charAt[1] = substr($string2replace, -4,1);
$charAt[0] = substr($string2replace, 0,1);
if($charAt[4] < 9)
{
$string2replace = substr_replace($string2replace,$charAt[4]+1,$charLength);
}
else
{
$charAt[4] = 0;
$string2replace = substr_replace($string2replace,$charAt[4],$charLength);
if($charAt[3] < 9)
{
$string2replace = substr_replace($string2replace,$charAt[3]+1,$charLength- 1,1);
}
else
{
$charAt[3] = 0;
$string2replace = substr_replace($string2replace,$charAt[3],$charLength-1,1);
if($charAt[2] < 9)
{
$string2replace = substr_replace($string2replace,$charAt[2]+1,$charLength-2,1);
}
else
{
$charAt[2] = 0;
$string2replace = substr_replace($string2replace,$charAt[2],$charLength-2,1);
if($charAt[1] < 9)
{
$string2replace = substr_replace($string2replace,$charAt[1]+1,$charLength-3,1);
}
else
{
$charAt[1] = 0;
$string2replace = substr_replace($string2replace,$charAt[1],$charLength-3,1);
}
if($charAt[0] < 'z')
{
$charAt[0] ++;
$string2replace = substr_replace($string2replace,$charAt[0],$charLength-4,1);
}
else
{
$charAt[0] = 'a';
$string2replace = substr_replace($string2replace,$charAt[0],$charLength-4,1);
}
}
}
}
return $string2replace;
}
$string2begin = 'A9999';
$generatedString = replaceChar($string2begin);
echo $string2begin . "<br />" . $generatedString;
?>