PHP generates a simple implementation method for specifying random strings _php Tips

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.