Skip to content

Instantly share code, notes, and snippets.

@joshhartman
Created May 27, 2011 12:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshhartman/995148 to your computer and use it in GitHub Desktop.
Save joshhartman/995148 to your computer and use it in GitHub Desktop.
Random Alphanumeric String Generator
<?php
function random_string($length, $max = false)
{
if (is_int($max) && $max > $length) {
$length = mt_rand($length, $max);
}
$output = '';
for ($i = 0; $i < $length; $i++) {
$which = mt_rand(0, 2);
if ($which === 0) {
$output .= mt_rand(0, 9);
} elseif ($which === 1) {
$output .= chr(mt_rand(65, 90));
} else {
$output .= chr(mt_rand(97, 122));
}
}
return $output;
}
echo random_string(32);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment