PHP Learning Note: Universal random string generation function (already packaged)

Source: Internet
Author: User

Do the verification code to use, and then the function is encapsulated, using the time to set 2 parameters:

$str set the string to be collected, such as:

$str = ' Efasfgzsrhftjxjxjhsrth ';

Then the string generated in the function will be randomly fetched from the Efasfgzsrhftjxjxjhsrth;

The $codeLen sets the random string to be generated, and sets 5 to generate 5 random strings.

Principle: Random fetch of strings, concatenation of strings

Effect:

Code:

<?php//mt_rand  get random number Mt_rand (min, max); $str = "ABCDEFGHIJKMNPQRSTUVWXYZ0123456789ABCDEFGHIGKLMNPQRSTUVWXYZ" ;//Set the randomly collected string $codelen= ' 5 ';//Set the generated random number of function  Str_rand ($STR, $codeLen) {    $rand = "";    for ($i =0; $i < $codeLen-1; $i + +) {        $rand. = $str [Mt_rand (0, strlen ($str)-1)];  such as: The random number is  : $str [+]    }   return $rand;} $code =str_rand ($str, $codeLen); Echo $code;
?>

  

PHP Learning Note: Universal random string generation function (already packaged)

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.