note 53780 deleted from function.rand by didou
| From: | didou@php.net | Date: | Mon, 13 Jun 2005 10:16:39 +0000 |
| Subject: | note 53780 deleted from function.rand by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-90346@lists.php.net to get a copy of this message | ||
Note Submitter:
Reason: bad code
----
To generate a string of unique characters this works for me.
$keychars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$length = 32;
$randkey = array();
$max=strlen($keychars)-1;
if($length>$max)
die("infinate loop");
for ($i=0;$i<=$length;$i++)
{
$newChar=substr($keychars, rand(0, $max), 1);
if(!in_array("$newChar",$randkey))
$randkey[]= "$newChar";
else
$i=count($randkey);
}
echo implode($randkey);