开发者

PHP ordered Unique ID

Hello I hope someone could help me cus Iam little bit confused about task I have to do in PHP I need php file that is unique registration ID with these parameters: First is AA00001 and next one is DF00002.

So first letter + 3 and second + 5, but numbers going in +1 order.

Coul开发者_Go百科d someone give me hint how to achieve this? Thank you!


In pseudocode:

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"

Note that you'll need to ensure that this happens atomically - i.e. that you don't have multiple processes working on the same ID, else they'd get the same "next" ID. This will IMHO be the hardest part.


You can use substr to split the ID, dechex and hexdec to convert to/from decimal to hexadecimal, which gives you the A+3=D part, and you can use str_pad to front pad the integer with zeros, which gives you the second part, and then you just concatenate them.

ETA: Something like this:

$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: Unless you meant to go AA00001, DF00002, GK00003, JP00004, MU00005, PZ00006, SE00007 etc in which case you need

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


$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


First you have to parse the last reg id.(using substr) Then, store each value in a var corresponding to the place $first , $second, $numberpart. then

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

THen update the record accordingly converting$first, $second to their appro. letters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜