仅使用az AZ 0

编程入门 行业动态 更新时间:2024-10-27 18:30:19
仅使用az AZ 0-9生成6个字符长的唯一ID(generating 6 characters long unique ID using only a-z A-Z 0-9)

我自己尝试编写这个函数,虽然它没有按预期工作,现在我看到了逻辑缺陷。

$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).

更多推荐

本文发布于:2023-07-26 02:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1269552.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:az   AZ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!