Skip to content

Instantly share code, notes, and snippets.

@stefanbc
Created February 21, 2014 07:44
Show Gist options
  • Save stefanbc/9130304 to your computer and use it in GitHub Desktop.
Save stefanbc/9130304 to your computer and use it in GitHub Desktop.
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;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment