-1

I am trying to generate random alphanumeric string with numbers from 0-9 and letters from a - f and have the following code:-

 <?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. They should not be seperated but mixed like this

 7de5a1a5ae85e3aef5376333c3410ca984ef56f0c8082f9d6703414c01afbec3

Any help is appreciated.

4 Answers4

1

You can do this by first add 25-26 alpha chars. 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

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
0

You may try this to generate random string which contains 26 letters and rest of are digits. 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);
   }
Shivani Sonagara
  • 1,299
  • 9
  • 21
0

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 Geek
  • 3,949
  • 1
  • 16
  • 32
0
<?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"
MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46