创建 14 个字符 'random key generator'

Creating a 14 character 'random key generator'

我正在尝试使用 CodeIgniter 为学校编写一个小程序,它会在我每次单击 'generate' 按钮时随机生成一个 'key'。看看是否有办法让我创建一个函数,我可以用一个随机数字或字母填充一个 14 个字符的数组,然后将该数组设置为一个变量,我可以调用它来显示我生成的键。

我将不胜感激,因为我是 CodeIgniter 的新手。

这取决于你想要的随机性。您可以在 $characters 字符串中指定您想要的所有字符,然后只需创建一个最多 $length 的字符串,从字符串中选择一个长度为 1 的随机子字符串。

有什么要求? 你想让它尽可能随机吗(This link might be useful) 是否允许在一个随机字符串中多次出现一个字符?

这里有一个例子:PHP random string generator

$a=array(rand(10000000000000, 99999999999999));

是获取 14 位数组的快速方法。

前段时间我在 PHP 中写了这个函数,它完成了它的功能并通过复杂性修饰符为您提供了一些灵活性,我使用了一组默认的 5 个不同的 'levels' 字符并且长度当然也是可变的。

我只是要把它放在这里 'try' 通过评论尽可能地解释发生了什么:

function rsg($length = 10, $complexity = 2) {
        //available 'complexity' subsets of characters
        $charSubSets = array(
            'abcdefghijklmnopqrstuvwxyz',
            'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
            '0123456789',
            '!@#$%^&*()_+{}|:">?<[]\\';,.`~',
            'µñ©æáßðøäåé®þüúíóö'
        );

        // will be filled with subsets from above $charSubsets
        $chars = '';

        //concact each subset until complexity is reached onto the $chars variable
        for ($i = 0; $i < $complexity; $i++)
            $chars .= $charSubSets[$i];

        //create array containing a single char per entry from the combined subset in the $chars variable.
        $chars = str_split($chars);
        //define length of array for mt_rand limit
        $charCount = (count($chars) - 1);
        //create string to return
        $string = '';
        //idk why I used a while but it won't really hurt you when the string is less than 100000 chars long ;)
        $i = 0;
        while ($i < $length) {
            $randomNumber = mt_rand(0, $charCount); //generate number within array index range
            $string .= $chars[$randomNumber]; //get that character out of the array
            $i++; //increment counter
        }

        return $string; //return string created from random characters
    }

这是我目前使用的,它已经满足了我很长一段时间的需求,如果有人阅读这篇文章有改进,我也很想听听他们的意见!