note 31302 deleted from function.rand by aidan
| From: | aidan@php.net | Date: | Sat, 10 Jul 2004 02:34:56 +0000 |
| Subject: | note 31302 deleted from function.rand by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72968@lists.php.net to get a copy of this message | ||
Note Submitter: vibemachine@hotmail.com
----
A simple function that generates a random string:
function str_makerand ($minlength, $maxlength, $useupper, $usespecial, $usenumbers)
{
/*
Author: Peter Mugane Kionga-Kamau http://mugane.homestake.net
Description: string str_makerand(int $minlength, int $maxlength, bool $useupper, bool $usespecial,
bool $usenumbers)
returns a randomly generated string of length between $minlength and $maxlength inclusively.
Notes:
- If $useupper is true uppercase characters will be used; if false they will be excluded.
- If $usespecial is true special characters will be used; if false they will be excluded.
- If $usenumbers is true numerical characters will be used; if false they will be excluded.
- If $minlength is equal to $maxlength a string of length $maxlength will be returned.
- Not all special characters are included since they could cause parse errors with queries. To use
all special characters change the if ($usespecial)... line below to read if
($usespecial)$charset="~!@#$%^&*()_+`-={}|\\]?[\":;'><,./"
Modify at will.
*/
$charset = "abcdefghijklmnopqrstuvwxyz";
if ($useupper) $charset .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if ($usenumbers) $charset .= "0123456789";
if ($usespecial) $charset .= "~@#$%^*()_+-={}|][";
if ($minlength > $maxlength) $length = mt_rand ($maxlength, $minlength);
else $length = mt_rand ($minlength, $maxlength);
for ($i=0; $i<$length; $i++) $key .= $charset[(mt_rand(0,strlen($charset)))];
return $key;
}