你好,我希望有人可以帮助我cus IAM有点困惑我要在PHP中做的任务 我需要php文件,这些参数是唯一注册ID: 首先是AA00001,接下来是DF00002 。PHP订购唯一ID
所以第一个字母+3和第二个+5,但数字以+1顺序进行。
有人能给我提示如何做到这一点? 谢谢!
你好,我希望有人可以帮助我cus IAM有点困惑我要在PHP中做的任务 我需要php文件,这些参数是唯一注册ID: 首先是AA00001,接下来是DF00002 。PHP订购唯一ID
所以第一个字母+3和第二个+5,但数字以+1顺序进行。
有人能给我提示如何做到这一点? 谢谢!
你可以使用substr来分割ID,dechex和十六进制来将十进制转换为十六进制,这会给你A + 3 = D部分,并且你可以使用str_pad前面填充整数零,给你第二部分,然后你把它们连接起来。
ETA:事情是这样的:
$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);
谢谢你,这是我需要的!目前正在努力使它继续增长按钮点击... – Emils
伪代码:
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。这将恕我直言,是最难的部分。
$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的一些问题 - 安全风险... – Emils
首先你要解析的最后一个REG ID。(使用SUBSTR)然后, 店在相应的地方 $first , $second, $numberpart
一个变种每个值。然后
$first = ($first + 3) % 16;
$second =($first + 5) % 16;
$number = $number + 1;
然后更新相应的转换$first, $second
他们Appro公司备案。字母。
你能告诉我们你已经尝试了什么,以及你的尝试失败了吗? –
那么,GK00003会是下一个?另外,我假设这些字母环绕? –
是的GK00003,它应该发生在按钮点击html ... – Emils