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 functions. below I will summarize them for you.

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:

  1. /* Generate Password
  2. * Length: 8
  3. */
  4. $ Str = "0123456789abcdefghijklmnopqrstuvwxyz"; // output character set
  5. $ N = 8; // output string length
  6. $ Len = strlen ($ str)-1;
  7. For ($ I = 0; $ I <$ n; $ I ++ ){
  8. $ S. = $ str [rand (0, $ len)];
  9. }
  10. Echo $ s."
    ";
  11. ?>

You can generate a string of numbers, character strings, and so on (uppercase, lowercase, uppercase, and lowercase, and a combination of uppercase and lowercase numbers can also be expanded as needed ).

The following $ length = 5 is 10 digits if you change it to 10.

Change $ str = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz 'to $ str = '000000'. the code is as follows:

  1. Function getRandStr ($ length ){
  2. $ Str = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz ';
  3. $ RandString = '';
  4. $ Len = strlen ($ str)-1;
  5. For ($ I = 0; $ I <$ length; $ I ++ ){
  6. $ Num = mt_rand (0, $ len );
  7. $ RandString. = $ str [$ num];
  8. }
  9. Return $ randString;
  10. }
  11. // Use the following method:
  12. $ Test = getRandStr ($ length = 5 );
  13. Echo $ test;
  14. ?>

Or use while, the code is as follows:

  1. /**
  2. */
  3. Function createRandomStr ($ length ){
  4. $ Str = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz '; // 62 characters
  5. $ Strlen = 62;
  6. While ($ length> $ strlen ){
  7. $ Str. = $ str;
  8. $ Strlen + = 62;
  9. }
  10. $ Str = str_shuffle ($ str );
  11. Return substr ($ str, 0, $ length );
  12. }
  13. Echo createRandomStr (10 );
  14. ?>

The code of array and character conversion is as follows:

  1. /**
  2. * @ Blog
  3. */
  4. Function createRandomStr ($ length ){
  5. $ Str = array_merge (range (0, 9), range ('A', 'z'), range ('A', 'z '));
  6. Shuffle ($ str );
  7. $ Str = implode ('', array_slice ($ str, 0, $ length ));
  8. Return $ str;
  9. }
  10. Echo createRandomStr (10 );
  11. ?>

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.