我自己尝试编写这个函数,虽然它没有按预期工作,现在我看到了逻辑缺陷。
$string = 'aaaaaa'; function hp_update_uid($string) { $position = strpos($string, '9'); if($position === 0) { return FALSE; } elseif($position === FALSE) { $position = -1; } else { $position = -(7-$position); } #var_dump($position); #exit; $character_ord = ord(substr($string, $position, 1)); if($character_ord == 122) { $character_ord = 65; } elseif($character_ord == 90) { $character_ord = 48; } else { ++$character_ord; } $string = substr_replace($string, chr($character_ord), $position, 1); return $string; } for($i = 0; $i < 1000; $i++) { $string = hp_update_uid($string); echo $string . '<br />'; }这将产生以下输出: https : //gist.github.com/937b148a126924b9429d
如何仅使用az AZ 0-9并使用逻辑增加来生成6个字符长的唯一ID?
I've tried writing the function myself, though it doesn't work as expected and now I see the logic flaw.
$string = 'aaaaaa'; function hp_update_uid($string) { $position = strpos($string, '9'); if($position === 0) { return FALSE; } elseif($position === FALSE) { $position = -1; } else { $position = -(7-$position); } #var_dump($position); #exit; $character_ord = ord(substr($string, $position, 1)); if($character_ord == 122) { $character_ord = 65; } elseif($character_ord == 90) { $character_ord = 48; } else { ++$character_ord; } $string = substr_replace($string, chr($character_ord), $position, 1); return $string; } for($i = 0; $i < 1000; $i++) { $string = hp_update_uid($string); echo $string . '<br />'; }This will produce the following output: https://gist.github.com/937b148a126924b9429d
How do I really generate 6 characters long unique ID using only a-z A-Z 0-9 and using logical increase?
最满意答案
如何仅使用az AZ 0-9并使用逻辑增加来生成6个字符长的唯一ID?
如果它们需要以1为步长增加,则从0开始,然后再增加1并将结果数字转换为base62([AZ] + 26中的[26]来自[az] + 10中的[0-9]。
当长度不再适合时停止( 62^6 - 1如果我没有误)。
How do I really generate 6 characters long unique ID using only a-z A-Z 0-9 and using logical increase?
If they need to increase by steps of 1, start at 0, then increase by 1 and convert the resulting number in base62 (26 from [A-Z] + 26 from [a-z] + 10 from [0-9]).
Stop when the length no longer fits (62^6 - 1 if I am not mistaking).
更多推荐
发布评论