PHP generates a simple implementation of the specified random string, PHP generates a string _php tutorial

Source: Internet
Author: User

PHP generates a simple implementation of a specified random string, and PHP generates a string


This article describes a simple implementation of 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 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);

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/977791.html www.bkjia.com true http://www.bkjia.com/PHPjc/977791.html techarticle PHP generates a simple implementation of a specified random string, and PHP generates a string This article describes a simple implementation of PHP to generate a specified random string. Share to everyone for your reference. ...

  • Related Article

    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.