Re: generate random ascii string
| From: | Philip Olson | Date: | Sat, 01 Sep 2001 16:54:34 +0000 |
| Subject: | Re: generate random ascii string | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-65612@lists.php.net to get a copy of this message | ||
Hi Bill,
Here's a quick hack, should get you on your way :
function randomString($len)
{
global $seed;
$tmp = '';
$chars = 'abcdefghijklmnopqrstuvwxyz';
$chars .= strtoupper($chars);
$chars .= '0123456789';
if (empty($seed)) {
mt_srand ((float) microtime() * 1000000);
$seed = TRUE;
}
// weak error checking mechanism
if ($len < 1 || !is_numeric($len)) $len = 8;
$chars_len = strlen($chars);
for ($a=1; $a <= $len; $a++) {
$tmp .= $chars{mt_rand(0,$chars_len)};
}
return $tmp;
}
That was fun :)
Regards,
Philip Olson
On Sat, 1 Sep 2001, bill wrote:
> How can a random string of only letters and numbers be generated?
>
> I'm stumped so far on how to avoid including symbols.
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>