简体   繁体   English

生成具有固定字母数的随机字母数字字符串

[英]Generate random alphanumeric string with fixed number of letters

I am trying to generate random alphanumeric string with numbers from 0-9 and letters from a - f and have the following code:- 我正在尝试使用0-9的数字和a-f的字母生成随机的字母数字字符串,并具有以下代码:-

 <?php
 function random_str($length, $keyspace = '0123456789abcdef')
{
$pieces = [];
$max = mb_strlen($keyspace, '8bit') - 1;
for ($i = 0; $i < $length; ++$i) {
    $pieces []= $keyspace[random_int(0, $max)];
}
return implode('', $pieces);
}

$a = random_str(64);
echo $a;
?>

But the problem it generates a string with number of letters randomly but I want a string which has 64 characters in total and must have either 26 or 25 letters in total and rest should be numbers. 但是问题是它随机生成一个带有多个字母的字符串,但是我想要一个总共有64个字符且必须总共有26个或25个字母的字符串,其余的应该是数字。 They should not be seperated but mixed like this 他们不应该分开,而是这样混合

 7de5a1a5ae85e3aef5376333c3410ca984ef56f0c8082f9d6703414c01afbec3

Any help is appreciated. 任何帮助表示赞赏。

You can do this by first add 25-26 alpha chars. 您可以通过先添加25-26个Alpha字符来实现。 Then you add the rest as numbers. 然后,将其余部分添加为数字。 When you're done, just shuffle the whole string: 完成后,只需将整个字符串随机播放即可:

function randomString()
{
    // Define the result variable
    $str   = '';

    // Generate an array with a-f
    $alpha = range('a', 'f');

    // Get either 25 or 26
    $alphaCount = rand(25, 26);

    // Add a random alpha char to the string 25 or 26 times.
    for ($i = 0; $i < $alphaCount; $i++) {
        $str .= $alpha[array_rand($alpha)];
    }

    // Check how many numbers we need to add
    $numCount = 64 - $alphaCount;

    // Add those numbers to the string
    for ($i = 0; $i < $numCount;  $i++) {
        $str .= rand(0, 9);
    }

    // Randomize the string and return it
    return str_shuffle($str);
}

Here's a demo: https://3v4l.org/4YfsS 这是一个演示: https : //3v4l.org/4YfsS

You may try this to generate random string which contains 26 letters and rest of are digits. 您可以尝试生成随机字符串,该字符串包含26个字母,其余为数字。 Hope so, this helps you!! 希望如此,对您有帮助!!

    function random_str() {
        $aplhabets = str_split('abcdefghijklmnopqrstuvwxyz');
        shuffle($aplhabets); // probably optional since array_is randomized; this may be redundant
        $rand_str = '';
        foreach (array_rand($aplhabets, 26) as $k)
            $rand_str .= $seed[$k];

        $digits = str_split('0123456789'); // and any other characters
        foreach (array_rand($digits, 38) as $j)
            $rand_str .= $digits[$j];

        return shuffle($rand_str);
   }

try this: 尝试这个:

function generate_random_string($type = 'alnum', $length = 16) {
        switch ($type) {
            case 'alpha' : $pool = 'ABCDEFGHJKLMNPQRSTUVWXYZ';
                break;
            case 'alnum' : $pool = '0123456789abcdef';
                break;
            case 'numeric' : $pool = '23456789';
                break;
        }
        $str = '';
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($pool, mt_rand(0, strlen($pool) - 1), 1);
        }
        return $str;
    }
<?php
function getName($n, $characters) {
    $randomString = '';

    for ($i = 0; $i < $n; $i++) {
        $index = rand(0, strlen($characters) - 1);
        $randomString .= $characters[$index];
    }

    return $randomString;
}

$result = '';
$letters = getName(25, 'abcdef');
$numbers = getName(39, '0123456789');
$result = str_shuffle($letters . $numbers);
var_dump($result);

?>

Return: 返回:

string(64) "65517e5b9910a15313695b1ebb3e53b56b47802afdcb4b0d3b141eb3cae8f2a7"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM