From 9bc81d1aed0f316e66e979bb94f03ef738f1d273 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 13 Feb 2005 00:07:54 +0000 Subject: [PATCH] Added random string generator. --- functions.php | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/functions.php b/functions.php index 75b2dc5..a136344 100644 --- a/functions.php +++ b/functions.php @@ -279,6 +279,71 @@ class Functions return false; } } + + /** + * Generates a random string of random length (unless otherwise specified) + * + * @param int Optional length + * + * @return str A random string + */ + function rand($length = 0) + { + // Gimme a length! + if (!$length) + { + $length = rand(20, 65); + } + + // Number of ints in our salt + $intcount = rand(0, intval($length / 2)); + + // Number of chars + $charcount = $length - $intcount; + + // Upper-case chars + $upperchars = rand(1, intval($charcount / 2)); + + // Lower-case chars + $lowerchars = $charcount - $upperchars; + + // Generate ints + for ($i = 0; $i < $intcount; $i++) + { + $string[] = rand(0, 9); + } + + // Generate upper chars + for ($i = 0; $i < $upperchars; $i++) + { + $string[] = chr(rand(65, 90)); + } + + // Generate lower chars + for ($i = 0; $i < $lowerchars; $i++) + { + $string[] = chr(rand(97, 122)); + } + + // Randomly key the chars + foreach ($string AS $char) + { + $rand = mt_rand(); + $newstr["$rand"] = $char; + } + + // Sort the chars by thier random assignment + ksort($newstr); + + // Flatten the array + $string = ''; + foreach ($newstr AS $char) + { + $string .= $char; + } + + return $string; + } } /*=====================================================================*\ -- 2.43.5