自动增加id

时间:2012-06-19 12:58:25

标签: php

我想构建一个php脚本,通过将先前的增加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;


?>

1 个答案:

答案 0 :(得分:3)

您的ID编号方案似乎颇为人为,其中高位数为A-Z,其余数字为0-9。如果我正确地理解了这种模式,那么这似乎可以解决问题:

function incrementID($id)
{
    $letter = $id[0];
    $number = substr($id, 1);

    $newNum = str_pad($number + 1, strlen($number), '0', STR_PAD_LEFT);

    // increase number only
    if (strlen($number) == strlen($newNum))
        return $letter . $newNum;

    // increase ID length ('Z' to 'A')
    if ($letter == 'Z')
        return 'A' . str_repeat('0', strlen($number) + 1);

    // change letter
    $newLetter = chr(ord($letter) + 1);
    return $newLetter . str_repeat('0', strlen($number));

}

printf("%s\n", incrementID('A0009')); // 'A0010'
printf("%s\n", incrementID('A9999')); // 'B0000'
printf("%s\n", incrementID('Z9999')); // 'A00000'

即使你的例子不合适,我首先假设你真的只想要一个基数为36的数字(任何数字可以是0-9,A-Z,其中A是10和Z是35)。使用base-36中的数字很容易,因为您可以使用base_convert()将它们转换为惯用的base-10。这就是增加base-36数字所需的全部内容:

function incrementBase36($id)
{
    $numVal = base_convert($id, 36, 10);
    $newId = base_convert($numVal + 1, 10, 36);
    return strtoupper($newId);
}

printf("%s\n", incrementBase36('A0009')); // 'A000A'
printf("%s\n", incrementBase36('A9999')); // 'A999A'
printf("%s\n", incrementBase36('Z9999')); // 'Z999A'
printf("%s\n", incrementBase36('AZZZZ')); // 'B0000'
printf("%s\n", incrementBase36('ZZZZZ')); // '100000'