Re: generate random ascii string

From: Date: Sat, 01 Sep 2001 18:05:31 +0000
Subject: Re: generate random ascii string
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-65613@lists.php.net to get a copy of this message
Philip, With a few tweaks, it works great, thanks! Here's what worked: 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 || !intval($len)) $len = 8; $chars_len = strlen($chars); for ($a=1; $a <= $len; $a++) { $tmp .= $chars[mt_rand(0,$chars_len)]; } return $tmp; } Philip Olson wrote: > 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 > >

« previous php.general (#65613) next »