This article mainly introduces the simple implementation of PHP to generate the specified random string, involving PHP operation array and string skills, with a certain reference value, the need for friends can refer to the following
This article describes a 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.
/** * @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); Determines whether the argument is valid if ($count!== count (Array_intersect ($case, $valid _type)) { return false; } according to the intersection) $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);
Summary : The above is the entire content of this article, I hope to be able to help you learn.
Related recommendations:
PHP implementation tips for copying and moving files
PHP based on acquisition class Snoopy implementation of the method of grasping Thunderbolt VIP account
PHP template and JS upload plug-in combination to implement a non-flush upload method