PHP generates random string program code

Source: Internet
Author: User

Because the tool requires us to write a function that can generate random strings. I think there should be a lot of such random string generation functions on the Internet, baidu has a bunch of good php functions below.

Use the for loop to traverse the defined characters.

The Code is as follows: Copy code


<? Php
/* 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. "<br/> ";
?>


It can generate a string of numbers, character strings, and so on.
(The combination of upper-case, lower-case, upper-case, and upper-case numbers can also be expanded based on your preferences ).

The following $ length = 5 is 10 digits if you change it to 10.
Change $ str = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz 'to $ str = '123', which is a pure numeric string.

The Code is as follows: Copy code

<? Php
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 use the while

The Code is as follows: Copy code

<? Php
/**
*/
Function createRandomStr ($ length ){
$ Str = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz '; // 62 characters
$ Strlen = 62;
While ($ length> $ strlen ){
$ Str. = $ str;
$ Strlen + = 62;
}
$ Str = str_shuffle ($ str );
Return substr ($ str, 0, $ length );
}
Echo createRandomStr (10 );

?>

The idea of array and character conversion is used:

The Code is as follows: Copy code

<? Php
/**
* @ Blog <www. bKjia. c0m>
*/
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 );
?>

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.