php 如何使用codeigniter生成5位字母数字唯一ID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4527237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate 5 digit alphanumeric unique ID using codeigniter?
提问by Maverick
I have a project in which I need to generate a unique 5 digit alphanumeric ID for the user. How can I achieve this using codeigniter!?
我有一个项目,需要为用户生成一个唯一的 5 位字母数字 ID。我怎样才能使用 codeigniter 实现这一点!?
thanks
谢谢
回答by navruzm
回答by ifaour
Since you need a unique id, then you should use a unique data from the user record (index for example) or just generate a random one and directly check it's occurrence in the DB.
由于您需要一个唯一的 id,那么您应该使用来自用户记录(例如索引)的唯一数据,或者只生成一个随机数据并直接检查它在数据库中的出现。
Here are the two approaches:
以下是两种方法:
回答by Mike C
Convert the user's numeric id (which I am assuming you already have) to base-36 using base_convert($id, 10, 36);
将用户的数字 id(我假设您已经拥有)转换为 base-36 base_convert($id, 10, 36);
回答by rajkumar
The below piece of code has to be written under calculation field in sharepoint list to generate combination alpha numeric item id dynamically in the incremental model.
以下代码必须写在共享点列表的计算字段下,以在增量模型中动态生成组合字母数字项 id。
=field1&"-"&LEFT("00000",5-(LEN(TEXT([field2],"0"))))&TEXT([field2],"0")
filed1=contains the prefix alpha value i.e (xyz,abc)
归档 1=包含前缀 alpha 值,即 (xyz,abc)
field2=contains the starting value of the field1 range i.e (1,10,20)
field2=包含field1范围的起始值即(1,10,20)
output: xyz-00001,abc-00010..
输出:xyz-00001,abc-00010..
回答by Farside
I would highly recommend to use UUIDfor such purposes, as you project definitely will grow into something more serious.
我强烈建议将UUID用于此类目的,因为您的项目肯定会变得更严重。
For example, here's how we do in our project UID generation:
例如,下面是我们在项目 UID 生成中的做法:
<?php
// ...
public function getUid($uid = null)
{
while (! $this->isValid($uid)) {
$uid = sprintf('%04x%04x%02x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xff));
}
return $uid;
}
or find out another thread, with more complicated algorithms of generating UUID v4
或者找出另一个线程,使用更复杂的生成UUID v4 的算法