Simple implementation of PHP to generate a specified random string
The specific analysis is as follows:
This is a simple function that does not force the generated content to be set. So when the resulting string length is small, there is a case where no type character is specified. Of course, the changes are also very simple, here do not add.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23 |
/**
* @param string $type
* @param $length
* @return String
*/
function RandomString ($type = "Number,upper,lower", $length) {
$valid _type = Array (' number ', ' upper ', ' lower ');
$case = Explode (",", $type);
$count = count ($case);
Determine whether a parameter is valid according to the intersection
if ($count!== count (Array_intersect ($case, $valid _type))) {
return false;
}
$lower = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$upper = Strtoupper ($lower);
$number = "0123456789";
$str _list = "";
for ($i =0; $i < $count; + + $i) {
$str _list. = $ $case [$i];
}
Return substr (Str_shuffle ($str _list), 0, $length);
}
Echo randomstring ("Number,upper,lower", 12);
|
http://www.bkjia.com/PHPjc/977615.html www.bkjia.com true http://www.bkjia.com/PHPjc/977615.html techarticle The simple implementation of PHP to generate a specified random string is analyzed as follows: This is a simple function that does not force the generated content to be set. So the resulting string length is more ...