PHP generate random String program code _php Tutorial

Source: Internet
Author: User
Because the tool needs us to write a can be random string generated function, this I think online should be a lot of this kind of generated random string function, Baidu a lot of the following I will summarize these good PHP functions for you.

Use the For loop to traverse our defined characters

The code is as follows Copy Code


/* Generate Password
* Length:8
*/
$str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; Output Character Set
$n = 8; Output string length
$len = strlen ($str)-1;
for ($i =0; $i < $n; $i + +) {
$s. = $str [rand (0, $len)];
}
Echo $s. “
”;
?>


You can generate pure numeric strings of a specified length, character strings, and so on.
(uppercase, lowercase, case, and case-to-number combinations can also be expanded to suit your preferences).

Below the $length=5, if you change to 10 is 10 bits. The
Change $str = ' abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz ' to $str = ' 0123456789 ' is a pure numeric string.

The code is as follows Copy Code

function Getrandstr ($length) {
$str = ' abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz ';
$randString = ";
$len = strlen ($str)-1;
for ($i = 0; $i < $length; $i + +) {
$num = Mt_rand (0, $len);
$randString. = $str [$num];
}
return $randString;
}

Use the following method
$test =getrandstr ($length =5);
Echo $test;
?>

or using the while

The code is as follows Copy Code

/**
*/
function Createrandomstr ($length) {
$str = ' 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ';//62 character (s)
$strlen = 62;
while ($length > $strlen) {
$str. = $str;
$strlen + = 62;
}
$str = Str_shuffle ($STR);
Return substr ($str, 0, $length);
}
Echo Createrandomstr (10);

?>

The idea of using arrays and character conversions:

The code is as follows Copy Code

/**
* @blog
*/
function Createrandomstr ($length) {
$str = Array_merge (range (0,9), Range (' A ', ' Z '), Range (' A ', ' Z '));
Shuffle ($STR);
$str = Implode (' ', Array_slice ($str, 0, $length));
return $str;
}
Echo Createrandomstr (10);
?>

http://www.bkjia.com/PHPjc/631543.html www.bkjia.com true http://www.bkjia.com/PHPjc/631543.html techarticle because the tool needs us to write a can be random string generated function, this I think online should be a lot of this kind of generated random string function, Baidu a lot of a bunch below I give ...

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