【问题标题】:PHP ordered Unique IDPHP 订购的唯一 ID
【发布时间】:2011-07-08 15:14:36
【问题描述】:

您好,我希望有人可以帮助我,因为我对我必须在 PHP 中完成的任务有点困惑 我需要具有以下参数的唯一注册 ID 的 php 文件: 第一个是AA00001,下一个是DF00002。

所以第一个字母 + 3 和第二个 + 5,但数字按 +1 顺序排列。

有人可以给我提示如何实现这一目标吗? 谢谢!

【问题讨论】:

  • 您能告诉我们您尝试了什么以及您的尝试失败的地方吗?
  • 那么,GK00003 会是下一个吗?另外,我假设字母环绕?
  • 是的 GK00003,这一切都应该发生在 html 中的按钮单击...

标签: php unique


【解决方案1】:

在伪代码中:

get previous ID
separate first letter, second letter and number
convert first letter to number, add 3, modulo 26, convert back to letter
convert second letter to number, add 5, modulo 26, convert back to letter
add 1 to number, add zero-padding to reach 5 digits
concatenate them all together
set this as the new "previous ID"

请注意,您需要确保这种情况自动发生 - 即您没有多个进程在同一个 ID 上工作,否则它们将获得相同的“下一个”ID。恕我直言,这将是最难的部分。

【讨论】:

    【解决方案2】:

    您可以使用 substr 拆分 ID,使用 dechex 和 hexdec 将十进制转换为/从十进制转换为十六进制,这为您提供 A+3=D 部分,您可以使用 str_pad 将整数前面填充零,这给出你是第二部分,然后你把它们连接起来。

    预计到达时间:像这样:

    $id = 'AA00001';
    
    $first = dechex((hexdec(substr($id,0,1))+3)%16);
    $secnd = dechex((hexdec(substr($id,1,1))+5)%16);
    $int = str_pad(substr($id,2)+1,5,"0",STR_PAD_LEFT);
    
    $newid = strtoupper($first.$secnd.$int);
    

    ETA2:除非你打算去 AA00001、DF00002、GK00003、JP00004、MU00005、PZ00006、SE00007 等,在这种情况下你需要

    $first = chr(((ord(substr($id,0,1))-62)%26)+65);
    $secnd = chr(((ord(substr($id,1,1))-60)%26)+65);
    

    【讨论】:

    • 谢谢,这正是我需要的!目前正在努力使其在按钮点击时继续增长......
    【解决方案3】:
    $lastid = 'AA00001';
    
    $first = substr($lastid, 0, 1);
    $second = substr($lastid, 1, 1);
    $numeric = substr($lastid, 2);
    
    $next_first = chr(((ord($first) - ord('A') + 3) % 26) + ord('A'));
    $next_second = chr(((ord($second) - ord('A') + 5) % 26) + ord('A'));
    $next_numeric = sprintf('%05d', intval($numeric) + 1);
    
    $new_id = $next_first . $next_second . $next_numeric;
    
    // DF00002
    

    【讨论】:

    • 这段代码运行良好!但是当尝试使用在线 php 预览对其进行测试时,它说明了 sprintf - 安全风险......
    【解决方案4】:

    首先你必须解析最后一个 reg id。(使用 substr)然后, 将每个值存储在与地点对应的 var 中 $first , $second, $numberpart。然后

    $first = ($first  + 3 ) % 16;
    $second =($first  + 5 ) % 16;
    $number = $number + 1;
    

    然后相应地更新记录,将$first, $second 转换为他们的批准。字母。

    【讨论】:

      猜你喜欢
      • 2017-02-09
      • 1970-01-01
      • 2023-04-03
      • 2011-08-27
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      • 2023-03-25
      • 2011-06-10
      相关资源
      最近更新 更多