From 04fb81367fd98c5eda0009b22b4202f6694ab388 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 15 Jun 2011 23:29:56 -0500 Subject: [PATCH] added support for alpha only random strings in Str::random. --- system/str.php | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/system/str.php b/system/str.php index 17370e7f..7ab40159 100644 --- a/system/str.php +++ b/system/str.php @@ -47,20 +47,43 @@ public static function title($value) } /** - * Generate a random alpha-numeric string. + * Generate a random alpha or alpha-numeric string. + * + * Supported types: 'alnum' and 'alpha'. * * @param int $length + * @param string $type * @return string */ - public static function random($length = 16) + public static function random($length = 16, $type = 'alnum') { - $pool = str_split('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 1); - $value = ''; + // ----------------------------------------------------- + // Get the proper character pool for the type. + // ----------------------------------------------------- + switch ($type) + { + case 'alpha': + $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + break; + + default: + $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + } + + // ----------------------------------------------------- + // Get the pool length and split the pool into an array. + // ----------------------------------------------------- + $pool_length = strlen($pool) - 1; + $pool = str_split($pool, 1); + + // ----------------------------------------------------- + // Build the random string to the specified length. + // ----------------------------------------------------- for ($i = 0; $i < $length; $i++) { - $value .= $pool[mt_rand(0, 61)]; + $value .= $pool[mt_rand(0, $pool_length)]; } return $value;