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;
?> |