stefanbc
2/21/2014 - 7:44 AM

Generate a random string

Generate a random string

<?php
  // Generate a random string with a given lenght and set special char on or off
  function random_gen($length, $special_char) {
  
    $random = "";
    srand((double)microtime()*1000000);
    $char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $char_list .= "abcdefghijklmnopqrstuvwxyz";
    $char_list .= "1234567890";
  
    if($special_char == true){
      $char_list .= "`~!@#$%^&*()-_=+,<.>/?;:[]{}\|";  
    }
  
    for($i = 0; $i < $length; $i++){
      $random .= substr($char_list,(rand()%(strlen($char_list))), 1);  
    }  
  
    return $random;
  
  }
?>