PHP encapsulation function implementation to generate random string verification code

Source: Internet
Author: User
Objective

In general, when we are doing the program, there will certainly be many places to use the random string, such as to do the verification code used, and then the function is encapsulated, use time to set 2 parameters, the principle is to randomly fetch the string, concatenation of the string

$str set the string to be collected, such as

$str =´jfowef34098094j3204efa234sfg2z23srhftj345xjxjhsrth´;

Then the string generated inside the function is randomly fetched from the $STR

$codeLen set the random string to be generated, set 5, then generate 5 random strings, such as

$codeLen =´5´;//Sets the number of random numbers generated

The code is as follows

<?php//mt_rand Gets the random number Mt_rand (min, max);//sets the randomly collected string $str= " ABCDEFGHIJKMNPQRSTUVWXYZ0123456789ABCDEFGHIGKLMNPQRSTUVWXYZ "; Sets the number of random numbers generated $codelen=´5´; function Str_rand ($STR, $codeLen) {$rand = ""; for ($i =0; $i < $codeLen-1; $i) {  //AS: Random number 30: $str [+]  $rand. = $ Str[mt_rand (0, strlen ($str)-1)]; } return $rand;} $code =str_rand ($str, $codeLen); Echo $code;?>

Summarize

The above is the complete method of generating random strings, in addition to the verification code can be used anywhere else, I hope that the content of this article on everyone's study or work can bring some help, if there are questions you can message exchange.

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.