A simple implementation method to generate a specified random string in PHP_php tips

墨辰丷
Release: 2023-03-31 16:28:01
Original
1408 people have browsed it

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!