note 90509 modified in function.rand by danbrown

From: Date: Sat, 25 Apr 2009 19:46:51 +0000
Subject: note 90509 modified in function.rand by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-153769@lists.php.net to get a copy of this message
A nice function to generate a random string, using any character: <?php function generateRandStr($length){ $randstr = ""; for($i=0; $i<$length; $i++){ $randnum = mt_rand(0,61); if($randnum < 10){ $randstr .= chr($randnum+48); }else if($randnum < 36){ $randstr .= chr($randnum+55); }else{ $randstr .= chr($randnum+61); } } return $randstr; } ?> Simply use: generateRandStr(10); Sample output: $%29zon(4f --was-- A nice function to generate a random string, using any character: function generateRandStr($length){ $randstr = ""; for($i=0; $i<$length; $i++){ $randnum = mt_rand(0,61); if($randnum < 10){ $randstr .= chr($randnum+48); }else if($randnum < 36){ $randstr .= chr($randnum+55); }else{ $randstr .= chr($randnum+61); } } return $randstr; } Simply use: generateRandStr(10); Sample output: $%29zon(4f http://php.net/manual/en/function.rand.php

« previous php.notes (#153769) next »