Skip to content

Instantly share code, notes, and snippets.

@mikedugan
Last active August 29, 2015 13:55
Show Gist options
  • Select an option

  • Save mikedugan/8734668 to your computer and use it in GitHub Desktop.

Select an option

Save mikedugan/8734668 to your computer and use it in GitHub Desktop.
random string
/*
* @param $chars ALPHA | NUM | ALPHANUM | HEX | string
* @param $length number
* @return $rstring
*/
public function GetRandomString($chars, $length)
{
switch ($chars) {
case "ALPHA":
$chars = "abcdefghijklmnopqrstuvwxyz";
break;
case "NUM":
$chars = "0123456789";
break;
case "ALPHANUM":
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
break;
case "HEX":
$chars = "abcdef0123456789";
break;
default:
break;
}
$rstring = "";
$n = strlen($chars);
for($i = 0; $i < $length; $i++)
{
$rstring .= $chars[mt_rand(1,$n) - 1];
}
return $rstring;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment