function getRandomString($length = 8) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= $characters[mt_rand(0, strlen($characters) - 1)];
}
return $string;
}
Answer from ThiefMaster on Stack Overflow Top answer 1 of 3
43
function getRandomString($length = 8) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= $characters[mt_rand(0, strlen($characters) - 1)];
}
return $string;
}
2 of 3
5
function randr($j = 8){
$string = "";
for($i=0; $i < $j; $i++){
$x = mt_rand(0, 2);
switch($x){
case 0: $string.= chr(mt_rand(97,122));break;
case 1: $string.= chr(mt_rand(65,90));break;
case 2: $string.= chr(mt_rand(48,57));break;
}
}
return $string;
}
echo randr(); // b53m1isM
Talkerscode
talkerscode.com › howto › php-generate-random-string-fixed-length.php
PHP Generate Random String Fixed Length - TalkersCode.com
In previous tutorials, we done that how we can generate a random number of fixed length whereas in this article we are going to done same task but using string here. Here, we already said that there are many ways. So, let us understand with most efficient way that is by creating a function and using for loop. Here below we are going to show you code in which we understand this method properly. <!DOCTYPE html> <html> <head> <title> php generate random string fixed length </title> </head> <body> <?php $n=8; function randomNumber($n) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $randomString = ''; for ($i = 0; $i < $n; $i++) { $index = rand(0, strlen($characters) - 1); $randomString .= $characters[$index]; } return $randomString; } echo randomNumber($n); ?> </body> </html>
Top answer 1 of 15
3
You can try this:
<?php
function random($len){
$char = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
// ----------------------------------------------
// Number of possible combinations
// ----------------------------------------------
$pos = strlen($char);
pos, $len);
echo
total = strlen($char)-1;
$text = "";
for (
i<
i++){
$text = $text.$char[rand(0, $total)];
}
return $text;
}
$string = random(15);
echo $string;
?>
You can also use md5 on time but be careful.
You need to use microtime() and not time() function, because if multiple threads run within the same second, you need to get different string for all of them.
<?php
$string = md5(microtime());
echo $string;
?>
2 of 15
3
//generateRandomString http://stackoverflow.com/questions/4356289/php-random-string-generator
function randomString($length = 32, $string= "0123456789abcdefghijklmnopqrstuvwxyz" ) {
//$string can be:
//0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
//0123456789abcdefghijklmnopqrstuvwxyz
return substr(str_shuffle( $string), 0, $length);
}
TecAdmin
tecadmin.net › generate-random-string-using-php
How To Generate Random String In PHP – TecAdmin
April 26, 2025 - This function has a limitation, the result string has a fixed length of 13 characters. ... The random_bytes() function generates a cryptographically secure pseudo-random byte of defined length. This result will be in binary format, which you can convert to a hex value using bin2hex() function.
CodeSpeedy
codespeedy.com › home › how to generate random string in php?
How to generate random string in PHP? - CodeSpeedy
June 5, 2019 - <?php $str = 'abcdefgh';//range for string generation $shufflestr = str_shuffle($str);//used to shuffle the range of strings echo $shufflestr; ?> ... The limitation of the above-predefined function is that you cannot specify more character sets than the given length. So, to overcome this, we need a user-defined function. Let us take an example of a user-defined function: <?php function RandomString($num) { // Variable that store final string $final_string = ""; //Range of values used for generating string $range = "_!@#$^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; // Find
Squash
squash.io › how-to-use-php-random-string-generator
How to Use the PHP Random String Generator - Squash Labs
To generate a random string using random_bytes(), you can use the following code: $length = 10; $randomString = bin2hex(random_bytes($length)); echo substr($randomString, 0, $length);
Experts PHP
expertsphp.com › how-to-generate-fixed-length-random-number-in-php
How to generate fixed length random number in php? - Experts PHP
October 4, 2021 - Today I will tell you how you can generate fixed random number Using PHP? First of all, you create an PHP file, add codes. Then run codes on the server show results.you can use rand() function for that in PHP.
GitHub
gist.github.com › karlgroves › 5227409
PHP Function to create random alphanumeric strings · GitHub
This can be fixed by editing line 7 from $randstr; to $randstr = ""; ... it works for me if u having error on randstr. then change to this function RandomString($length = 32) { $randstr = ' '; ...
Envato Tuts+
code.tutsplus.com › generate-random-alphanumeric-strings-in-php--cms-32132t
Generate Random Alphanumeric Strings in PHP | Envato Tuts+
October 29, 2021 - Whether you need to create an email form, add a shopping cart to your eCommerce store, or create a contact form with validation, there's a PHP script that's...