如何在laravel 5中创建唯一的随机字符串 [英] How can I create a unique random string in laravel 5

查看:2235
本文介绍了如何在laravel 5中创建唯一的随机字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是laravel 5的新手.我正在一个项目中,我想为每个应用程序分配一些随机可读的 unique 字符串.我知道每个应用程序ID都可以用作种子.由于该应用程序将在公司内部使用,因此我不必担心安全性.我希望表的大小会增加,所以我的目标是尽可能地实现唯一性,因为DB中的字段是唯一的.类似于(EN1A20,EN12ZOV等)的代码.如果该函数可以让我传递要返回的字符串的长度,那真是太棒了.

I am new to laravel 5. I am working on a project where I want to assign some random-readable unique string to each application. I have knowledge of the each application id which may be use as a seed. Since the app is going to be use within the company I don't worry much about security. I expect the table size to grow so my goal is to achieve uniqueness as much as possible because the field in DB is unique. A code like (EN1A20, EN12ZOV etc). If the function can allow me to pass the length of the string I want to return, that would be really awesome.

修改 下面显示的是我对问题的尝试

Edit Shown below is my attempt to the problem

private function generate_app_code($application_id) { 
        $token = $this->getToken(6, $application_id);
        $code = 'EN'. $token . substr(strftime("%Y", time()),2);

        return $code;
    }

    private function getToken($length, $seed){    
        $token = "";
        $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $codeAlphabet.= "0123456789";

        mt_srand($seed);      // Call once. Good since $application_id is unique.

        for($i=0;$i<$length;$i++){
            $token .= $codeAlphabet[mt_rand(0,strlen($codeAlphabet)-1)];
        }
        return $token;
    }

上面的代码能达到目的吗?

Can the code above do the trick?

修改

实际上,我是从这篇文章中借用的想法 PHP:如何生成一个随机的,唯一的字母数字字符串?来实现上述方法,但该帖子并未完全解决我的问题.我的目标是生成长度为6到8(字母数字和可读)的字符串.此字符串将由我的管理员用于查询目的.在我的函数中,我有mt_srand($ seed)用来为随机数生成器提供种子,其中seed是我的application_id.可能会获得重复的$ token.

Actually I borrowed ideas from this post PHP: How to generate a random, unique, alphanumeric string? to come out with the methods above but the post does not entirely address my issues. My goal is to generate a string of length say 6 to 8 (Alphanumeric and readable). This string would be use by my admin for query purposes. In my function I have mt_srand($seed) to seed the random number generator where seed is my application_id. It is possible to get duplicate $token.

感谢帮助.

推荐答案

尝试解决该问题时,您可以应用以下内容以确保唯一的代码:

With your attempt to the problem you could apply the following to ensure a unique code:

do
{
    $token = $this->getToken(6, $application_id);
    $code = 'EN'. $token . substr(strftime("%Y", time()),2);
    $user_code = User::where('user_code', $code)->get();
}
while(!empty($user_code));

修改

要避免laravel中出现无限循环,请使用

To avoid an infinite loop in laravel, use

do
    {
        $token = $this->getToken(6, $application_id);
        $code = 'EN'. $token . substr(strftime("%Y", time()),2);
        $user_code = User::where('user_code', $code)->get();
    }
    while(!$user_code->isEmpty());

http://laravel.com/api/5.0/Illuminate/Support/Collection.html#method_isEmpty

或选择

  do
        {
            $token = $this->getToken(6, $application_id);
            $code = 'EN'. $token . substr(strftime("%Y", time()),2);
            $user_code = User::where('user_code', $code)->first();
        }
        while(!empty($user_code));

使用first()代替get(). $ user_code可能是唯一的,因此我们可以方便地提取第一个结果.

Instead of get(), use first(). $user_code is probably unique so we can conveniently pull out the first result.

这篇关于如何在laravel 5中创建唯一的随机字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆