Method for generating random strings in PHP

Source: Internet
Author: User

 

Method for generating random strings in PHP:

In some special applications, You need to generate random strings, such as system random passwords or login verification codes. The function described in this article can return a random string of the specified length, which contains uppercase and lowercase letters and numbers by default, you can easily modify it to meet your needs.
<? PHP

// Description: Method for generating random strings in PHP
// Finishing: http://www.CodeBit.cn

Function genrandomstring ($ Len)
{
$ Chars = array (
"A", "B", "C", "D", "E", "F", "g", "H", "I", "J ", "K ",
"L", "M", "n", "O", "P", "Q", "r", "S", "T", "U ", "V ",
"W", "X", "Y", "Z", "A", "B", "C", "D", "E", "F ", "G ",
"H", "I", "J", "k", "L", "M", "n", "O", "P", "Q ", "r ",
"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1 ", "2 ",
"3", "4", "5", "6", "7", "8", "9"
);
$ Charslen = count ($ chars)-1;
Shuffle ($ chars); // disrupt the Array
$ Output = "";
For ($ I = 0; $ I <$ Len; $ I ++)
{
$ Output. = $ chars [mt_rand (0, $ charslen)];
}

Return $ output;
}
$ STR = genrandomstring (25 );
$ Str. = "<br/> ";
$ Str. = genrandomstring (25 );

$ Str. = "<br/> ";
$ Str. = genrandomstring (25 );
Echo $ STR;
?>

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.