生成字母数字唯一编号

发布于 2024-12-21 08:18:54 字数 220 浏览 1 评论 0原文

我想生成字母数字唯一数字,但格式应该是这样的

,应该从 AA001 到 AA999 开始,然后 AB001 到 AB999 .... BA001 到 BA999 以 ZZ999 结尾。如果我提供意见,

  1 = result AA001
 999  = result AA999
 1000 = result AB001 

有人可以帮忙吗?

I want to generate alphanumeric unique numbers but the format should be like this

that should be starts from AA001 to AA999 after that AB001 to AB999 .... BA001 to BA999 end with ZZ999. if i give the input is

  1 = result AA001
 999  = result AA999
 1000 = result AB001 

any one can help this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彡翼 2024-12-28 08:18:54

完整的解决方案(查看其运行):

function formatNum1000($num) {
  $tail =       $num % 1000;
  $head = (int)($num / 1000);
  $char1 = chr(ord('A') + (int)($head / 26));
  $char2 = chr(ord('A') +      ($head % 26));

  return sprintf('%s%s%03d', $char1, $char2, $tail);
}

function formatNum999($num) {
  $tail =      (($num - 1    ) % 999) + 1;
  $head = (int)(($num - $tail) / 999);
  $char1 = chr(ord('A') + (int)($head / 26));
  $char2 = chr(ord('A') +      ($head % 26));

  return sprintf('%s%s%03d', $char1, $char2, $tail);
}

$ns = array(1, 500, 999, 1000, 1998, 1999, 2000, 25974, 25975, 25999, 26000, 675324, 675999);
foreach($ns as $n) {
  $formatted1000 = formatNum1000($n);
  $formatted999  = formatNum999 ($n);
  echo "Num: $n => $formatted1000 / $formatted999\n";
}

注意您需要确保输入的数字在有效范围内(包含 000 数字时为 0...675999,否则为 1...675324)

注意:答案已修改,之前错过了要点那个 000 不是允许

Complete solution (see it running):

function formatNum1000($num) {
  $tail =       $num % 1000;
  $head = (int)($num / 1000);
  $char1 = chr(ord('A') + (int)($head / 26));
  $char2 = chr(ord('A') +      ($head % 26));

  return sprintf('%s%s%03d', $char1, $char2, $tail);
}

function formatNum999($num) {
  $tail =      (($num - 1    ) % 999) + 1;
  $head = (int)(($num - $tail) / 999);
  $char1 = chr(ord('A') + (int)($head / 26));
  $char2 = chr(ord('A') +      ($head % 26));

  return sprintf('%s%s%03d', $char1, $char2, $tail);
}

$ns = array(1, 500, 999, 1000, 1998, 1999, 2000, 25974, 25975, 25999, 26000, 675324, 675999);
foreach($ns as $n) {
  $formatted1000 = formatNum1000($n);
  $formatted999  = formatNum999 ($n);
  echo "Num: $n => $formatted1000 / $formatted999\n";
}

Note: you need to make sure that the input number is within the valid range (0...675999 when including 000-numbers, 1...675324 otherwise)

Note: answer revised, missed the point earlier that 000 is not allowed

雅心素梦 2024-12-28 08:18:54

怎么样:

$start = 'AA997';
for($i = 0; $i < 5; $i++) {
    $start++;
    if (substr($start, 2) == '000') continue;
    echo $start,"\n";
}

输出:

AA998
AA999
AB001
AB002

How about:

$start = 'AA997';
for($i = 0; $i < 5; $i++) {
    $start++;
    if (substr($start, 2) == '000') continue;
    echo $start,"\n";
}

output:

AA998
AA999
AB001
AB002
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文