PHP learning notes: universal random string generation function (encapsulated), php learning notes

Source: Internet
Author: User

PHP learning notes: universal random string generation function (encapsulated), php learning notes

The verification code is used, and then the function is encapsulated. You need to set two parameters when using the function:

$ Str: the string to be collected, for example:

$ Str = 'efasfgzsrhftjxjxjhsrth ';

Then the string generated in the function will be randomly crawled from efasfgzsrhftjxjhsrth;

$ CodeLen: Specifies the random string to be generated. If it is set to 5, five random strings are generated.

 

Principle: Randomly capture and splice strings

Effect:

Code:

<? Php // mt_rand obtain the random number mt_rand (min, max); $ str = "random"; // set the string to be randomly collected $ codeLen = '5 '; // set the number of random numbers generated function str_rand ($ str, $ codeLen) {$ rand = ""; for ($ I = 0; $ I <$ codeLen-1; $ I ++) {$ rand. = $ str [mt_rand (0, strlen ($ str)-1)]; // For example, if the random number is 30, return $ rand ;} $ code = str_rand ($ str, $ codeLen); echo $ code;
?>

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.