note 94788 deleted from function.rand by danbrown

From: Date: Tue, 02 Feb 2010 23:35:27 +0000
Subject: note 94788 deleted from function.rand by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-165610@lists.php.net to get a copy of this message
Note Submitter: bundyxc ---- genRandStr() creates a random string, with parameters to control length, type of characters, and also the ability to create an array of these randomized strings with the established properties (as opposed to running the function multiple times, which would require program to loop through the logic portion of the function again and again). <?php function genRandStr($minLen, $maxLen, $alphaLower = 1, $alphaUpper = 1, $num = 1, $batch = 1) { $alphaLowerArray = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'); $alphaUpperArray = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); $numArray = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); if (isset($minLen) && isset($maxLen)) { if ($minLen == $maxLen) { $strLen = $minLen; } else { $strLen = rand($minLen, $maxLen); } $merged = array_merge($alphaLowerArray, $alphaUpperArray, $numArray); if ($alphaLower == 1 && $alphaUpper == 1 && $num == 1) { $finalArray = array_merge($alphaLowerArray, $alphaUpperArray, $numArray); } elseif ($alphaLower == 1 && $alphaUpper == 1 && $num == 0) { $finalArray = array_merge($alphaLowerArray, $alphaUpperArray); } elseif ($alphaLower == 1 && $alphaUpper == 0 && $num == 1) { $finalArray = array_merge($alphaLowerArray, $numArray); } elseif ($alphaLower == 0 && $alphaUpper == 1 && $num == 1) { $finalArray = array_merge($alphaUpperArray, $numArray); } elseif ($alphaLower == 1 && $alphaUpper == 0 && $num == 0) { $finalArray = $alphaLowerArray; } elseif ($alphaLower == 0 && $alphaUpper == 1 && $num == 0) { $finalArray = $alphaUpperArray; } elseif ($alphaLower == 0 && $alphaUpper == 0 && $num == 1) { $finalArray = $numArray; } else { return FALSE; } $count = count($finalArray); if ($batch == 1) { $str = ''; $i = 1; while ($i <= $strLen) { $rand = rand(0, $count); $newChar = $finalArray[$rand]; $str .= $newChar; $i++; } $result = $str; } else { $j = 1; $result = array(); while ($j <= $batch) { $str = ''; $i = 1; while ($i <= $strLen) { $rand = rand(0, $count); $newChar = $finalArray[$rand]; $str .= $newChar; $i++; } $result[] = $str; $j++; } } return $result; } } print_r(genRandStr(5, 10, 1, 0, 1, 1)); //Output is a single string 5-10 characters long, using solely lowercase and numbers. print('<br /><br />'); print_r(genRandStr(8, 8, 0, 1, 0, 50)); //Output is an array containing 50 strings that are only uppercase, and 8 characters long. print('<br /><br />'); print_r(genRandStr(5, 10)); //Output is a single string 5-10 characters long, using the full alphabet (uppercase and lowercase), and all numbers. /* genRandStr($minLen, $maxLen, $alphaLower = 1, $alphaUpper = 1, $num = 1, $batch = 1) $minLen is the minimum length of the string. Required. $maxLen is the maximum length of the string. Required. $alphaLower toggles the use of lowercase letters (a-z). Default is 1 (lowecase letters may be used). $alphaUpper toggles the use of uppercase letters (A-Z). Default is 1 (uppercase letters may be used). $num toggles the use of numbers (0-9). Default is 1 (numbers may be used). $batch specifies the number of strings to create. Default is 1 (returns one string). When $batch is not 1 the function returns an array of multiple strings. */ ?>

« previous php.notes (#165610) next »