How to generate random string in PHP

9 Answers

0 votes
function generateRandomString($length = 6) {
    $all_characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_';
    $count_characters = strlen($all_characters);
    $random_string = '';
    
    for ($i = 0; $i < $length; $i++) {
        $random_string .= $all_characters[rand(0, $count_characters - 1)];
    }
     
    return $random_string;
}
 
echo generateRandomString(10) . "\n";
 
echo generateRandomString() . "\n";
 
 
 
/*
run:
  
mdGUNBv5jZ
qhiczU
  
*/

 



answered Apr 1, 2016 by avibootz
edited Apr 14, 2024 by avibootz
0 votes
function generateRandomString($length = 6) {
    $s = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"), 
                0, 
                $length);
    return $s;
}
 
echo generateRandomString(10) . "\n";
 
echo generateRandomString() . "\n";
 
 
 
/*
run:
  
UT5geCN1jG
Y13Ssm
  
*/

 



answered Apr 1, 2016 by avibootz
edited Apr 14, 2024 by avibootz
0 votes
echo substr(str_shuffle(MD5(microtime())), 0, 10);
 

/*
run:
  
d4fe077f90
  
*/

 



answered Apr 1, 2016 by avibootz
edited Apr 14, 2024 by avibootz
0 votes
function GenerateRandomString($length = 5, 
                              $abc = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_!@#$%')
{
    $s = '';
    
    $total_characters = mb_strlen($abc, '8bit') - 1;
    
    for ($i = 0; $i < $length; $i++) {
        $s .= $abc[rand(0, $total_characters)];
    }
  
    return $s;
}
 
echo generateRandomString(10) . "\n";
 
echo generateRandomString() . "\n";
 
 
 
/*
run:
  
uDmq8HeUAE
LtFK$
  
*/

 



answered Apr 4, 2016 by avibootz
edited Apr 14, 2024 by avibootz
0 votes
function GenerateRandomString($len) {
    srand(time()); 
    
    $abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()"; 
    $s = ""; 
    
    while (strlen($s) < $len) { 
        $s .= substr($abc,(rand()%(strlen($abc))), 1); 
    } 
   
    return $s; 
}
 
echo generateRandomString(10) . "\n";

 
 
/*
run:
  
X^i1bBV6$h
  
*/

 



answered Oct 24, 2016 by avibootz
edited Apr 14, 2024 by avibootz
0 votes
function GenerateRandomString($total_chars) { 
    $s = ""; 
 
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; 
 
    $chars_len = strlen($chars); 
       
    for ($i = 0; $i < $total_chars; $i++) { 
        $chars_index = rand(0, $chars_len - 1); 
           
        $s = $s . $chars[$chars_index]; 
    } 
       
    return $s; 
} 
   
echo GenerateRandomString(9); 
       
  
/*
run:
  
OdEgKM8Gc 
     
*/

 



answered Apr 14, 2024 by avibootz
0 votes
function GenerateRandomString($total_chars) { 
 
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; 
 
    return substr(str_shuffle($chars), 0, $total_chars); 
} 
   
echo GenerateRandomString(9); 
       
  
/*
run:
  
DMzWHtV80
     
*/

 



answered Apr 14, 2024 by avibootz
0 votes
function GenerateRandomString($total_chars) { 
    return substr(sha1(time()), 0, $total_chars);
} 
   
echo GenerateRandomString(9); 
       
  
/*
run:
  
26cec87b6 
     
*/

 



answered Apr 14, 2024 by avibootz
0 votes
function generateRandomString($length = 10) {
    return substr(bin2hex(random_bytes($length)), 0, $length);
}

echo generateRandomString(8);



/*
run:

a9c89af6

*/

 



answered Nov 4, 2024 by avibootz
...