This article mainly introduces the simple implementation method of generating specified random strings in PHP, involving the skills of operating arrays and strings in PHP. It has certain reference value. Friends who need it can refer to the examples of this article.
Describes a simple implementation method for PHP to generate a specified random string. The specific analysis is as follows:
This is a simple function, and there is no mandatory setting for the generated content. Therefore, when the length of the generated string is small, there will be situations where there are no specified type characters. Of course, it is very simple to modify, so I won’t add it here.
/** * @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); //根据交集判断参数是否合法 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);
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
php tips for copying and moving files
php is based on the collection class Snoopy's method of capturing Thunder VIP accounts
The method of combining php template and js upload plug-in to achieve refresh-free upload
The above is the detailed content of A simple implementation method to generate a specified random string in PHP_php tips. For more information, please follow other related articles on the PHP Chinese website!