PHP randomly generated string some methods summarize _php tutorial

Source: Internet
Author: User
I've talked about generating random passwords, so let's introduce some of the usual functions that generate random strings, these are our custom functions, and of course we have the system's own functions, but it's easier.

Mt_rand function

Example

In this case, we will return some random numbers:

The code is as follows Copy Code

Echo (Mt_rand ());
Echo (Mt_rand ());
Echo (Mt_rand (10,100));
?>
The output is similar to:

3150906288
513289678
35

Let's take a look at an example of the Mt_rand function.

The code is as follows Copy Code

function Roll () {
Return Mt_rand (1,6);
}

echo Roll ();

function Roll ($sides) {
Return Mt_rand (1, $sides);

}
echo Roll (6); Roll a six-sided die
echo Roll (10); Roll a ten-sided die
echo Roll (20); Roll a twenty-sided die

The above can only generate simple pure numbers, not letters or numbers and letters, we need to customize the function below

The code is as follows Copy Code

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); To disrupt an array

$output = "";
for ($i =0; $i < $len; $i + +) {
$output. = $chars [Mt_rand (0, $charsLen)];
}

return $output;
}

$str = genrandomstring (25);
$str. = "
";
$str. = genrandomstring (25);
$str. = "
";
$str. = genrandomstring (25);
$str. = "

";

Echo $str;
?>


The program output is as follows:


Dmlvamdkejz8whxrcnwzvanlb
Bilzsa19yyusvcr17krrzsoko
Inlwlqf0gsabn3l589y9s16gg

Cases

The default generated random string length is 5, and the resulting string contains: Numeric + uppercase

function function:

1. Generate a random string of the specified length

2. Flexibility to select the complexity of generated random strings

The code is as follows Copy Code

/**
+----------------------------------------------------------
* Generate random string
+------------------------- ---------------------------------
* @param int $length The random string length to be generated
* @param string $type random code type: 0, digit + capital letter, 1, number; 2, lowercase letters, 3, uppercase letters, 4, special characters;-1, digits + uppercase and lowercase letters + special characters
+----------------------------------------------------------
* @return String
+----------------------------------------------------------
*/
Function Randcode ($length = 5, $ Type = 0) {
$arr = array (1 = "0123456789", 2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 3 = "abcdefghijklmnopqrstuvwx YZ ", 4 =" ~@#$%^&* () {}[]| ");
if ($type = = 0) {
Array_pop ($arr),
$string = Implode (",", $arr);
} else if ($type = = "1") {
$string = Implode (",", $arr);
} else {
$string = $arr [$type];
}
$count = strlen ($string)-1;
for ($i = 0; $i < $length; $i + +) {
$str [$i] = $string [rand (0, $count)];
$code. = $str [$i];
}
return $code;
}

Cases

1, preset a character array $chars, including a–z,a–z,0–9, and some special characters
2. Randomly select $length elements from the array $chars by Array_rand ()
3, according to the obtained key an array group $keys, from the array $chars remove the character stitching string. The disadvantage of the method is that the same characters are not repeated.

The code is as follows Copy Code

function Make_password ($length = 8)
{
Password character set, you can add any character you need
$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 ', '! ',
' @ ', ' # ', ' $ ', '% ', ' ^ ', ' & ', ' * ', ' (', ') ', '-', ' _ ',
' [', '] ', ' {', '} ', ' < ', ' > ', ' ~ ', ' ', ' + ', ' = ', ', ',
'.', ';', ':', '/', '?', '|');

Randomly take $length number of element key names in the $chars
$keys = Array_rand ($chars, $length);

$password = ";
for ($i = 0; $i < $length; $i + +)
{
Concatenate $length array elements into a string
$password. = $chars [$keys [$i]];
}

return $password;
}

http://www.bkjia.com/PHPjc/631493.html www.bkjia.com true http://www.bkjia.com/PHPjc/631493.html techarticle before I tell you about generating random passwords, let me show you some common functions that generate random strings, these are our custom functions, and of course we have the system comes with a letter ...

  • 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.