Several methods for generating random strings in PHP

Source: Internet
Author: User
# Method 1functionmake_password ($ length8) {$ rangearray_merge (range (a, z), range (A, Z), range (0, 9); shuffle ($ range ); $ rangearray_slice ($ range, 0, $ length); $ passwordimplode (, $ range); return $ password;} rating:

# Method 1 function make_password ($ length = '8') {$ range = array_merge (range ('A', 'z'), range ('A ', 'Z'), range ('0', '9'); shuffle ($ range); $ range = array_slice ($ range, 0, $ length ); $ password = implode ('', $ range); return $ password;} rating:

# Method 1
function make_password( $length = '8'){    $range = array_merge( range('a', 'z') , range('A', 'Z') , range('0','9'));    shuffle($range);    $range = array_slice($range,0, $length);    $password = implode('' , $range);    return $password;}
Rating: It looks comfortable and can be customized as needed. The disadvantage is low efficiency. # Method 2
Function generate_password ($ length = 8) {// you can add the character set $ chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 as needed! @ # $ % ^ & * ()-_ [] {}<> ~ '+ = ,.;:/? | '; $ Password = ''; $ chars_max_index = strlen ($ chars)-1; for ($ I = 0; $ I <$ length; $ I ++) {// two character acquisition methods are provided here. // The first method is to use substr to intercept any character in $ chars; // The second type is to take any element of the character array $ chars // $ password. = substr ($ chars, mt_rand (0, $ chars_max_index), 1); $ password. = $ chars [mt_rand (0, $ chars_max_index)];} return $ password ;}
Rating: efficient, because only one mt_rand()Function. You can customize the character set as needed, but it looks messy. # Method 3
function create_password($pw_length = 8){    $randpwd = '';    for ($i = 0; $i < $pw_length; $i++)    {        $randpwd .= chr(mt_rand(33, 126));    }    return $randpwd;}
Rating: very efficient, with ASCII code and mt_rand(), chr()Complete the random string. You can customize the character set as needed. # Method 4
function get_password( $length = 8 ){    $str = substr(md5(time()), 0, 6);    return $str;}
Comment: Nima.

Original article address: several methods for generating random strings in PHP. Thank you for sharing it with the original author.

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.