note 94788 added to function.rand
| From: | bundyxc at osu1 dot php dot net | Date: | Wed, 25 Nov 2009 03:26:09 +0000 |
| Subject: | note 94788 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-163258@lists.php.net to get a copy of this message | ||
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.
*/
?>
----
Server IP: 192.41.40.227
Probable Submitter: 97.115.119.163
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- https://master.php.net/note/edit/94788
Del: integrated -- https://master.php.net/note/delete/94788/integrated
Del: useless -- https://master.php.net/note/delete/94788/useless
Del: bad code -- https://master.php.net/note/delete/94788/bad+code
Del: spam -- https://master.php.net/note/delete/94788/spam
Del: non-english -- https://master.php.net/note/delete/94788/non-english
Del: in docs -- https://master.php.net/note/delete/94788/in+docs
Del: other reasons-- https://master.php.net/note/delete/94788
Reject -- https://master.php.net/note/reject/94788
Search -- https://master.php.net/manage/user-notes.php