This article illustrates a simple way to implement PHP to generate a specified random string. Share to everyone for your reference. The specific analysis is as follows:
This is a simple function that does not enforce the generated content. So when the generated string is less long, there is a case where no type character is specified. Of course, the modification is also very simple, here do not add.
/**
* @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 if the parameter is valid based on intersection
($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);
I hope this article will help you with your PHP program design.