String with random number and alphabets

Generate an unpredictable alphanumeric string

For passwords, reset tokens and other values that must be hard to guess, choose characters with random_int() or encode random_bytes(). The older rand()-based generator below is retained only as a non-security legacy example.

$alphabet = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
$result = '';
for ($i = 0; $i < 12; $i++) {
    $result .= $alphabet[random_int(0, strlen($alphabet) - 1)];
}
echo $result;
We can generate a set of random digits by combining a set of alphabets and numbers. After this our random string will have a random combination of alphabets and numbers. You can read how to generate random numbers by reading math random generator and how to generate random alphabets here .

We will use these two methods to generate random numbers and random characters. We will add ( or combine ) these two to get our random string. Here to get a combination we will use random methods so the positions of numbers and alphabets are not fixed.

This updated generator uses random_int() so the generated characters are selected with a cryptographically secure random integer source.
Reload to generate another example
F3YS6QJJZD
The generator can be kept inside a function and given the required output length as an argument. So the length of the random string can be managed based on the script design.

Here is the full function with detail explanation with it ( read the comments )
<?Php
function random_generator($digits){
$alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ23456789";
$random_generator = "";
for($i=0; $i<$digits; $i++){
$random_generator .= $alphabet[random_int(0, strlen($alphabet)-1)];
}
return $random_generator;
}

echo random_generator(10);
?>
STRING REFERENCE how to change all the letters to upper case


Subscribe to our YouTube Channel here



plus2net.com







arman patel

26-05-2009

very comprencive and use full tutorial for me thx.
Harry

29-07-2009

WOW thanks for this!
bright-tutorials.com

31-12-2009

This is another method to do it, but there are others. Good job using mt_rand not many use it!
ketan

08-03-2010

good code for beginner
sattar

02-10-2012

easy and simple tutor. are available here
Php Programmer

10-08-2017

Great!! This tutorial was really helpful to create a random number and alphabets. Simple but effective.

Thanks.




PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer