包含 a-z、A-Z、0-9,如果要追加符号,按序增加在数组$string_arr内,并修改mt_rand(1, 62)的第二个参数为增加后的数组的最大键值。

<?php
function rand_string($lenght){
    $string_arr = [
        1 => 'a', 2 => 'b', 3 => 'c', 4 => 'd', 5 => 'e', 6 => 'f', 7 => 'g',
        8 => 'h', 9 => 'i', 10 => 'j', 11 => 'k', 12 => 'l', 13 => 'm', 14 => 'n',
        15 => 'o', 16 => 'p', 17 => 'q', 18 => 'r', 19 => 's', 20 => 't', 21 => 'u',
        22 => 'v', 23 => 'w', 24 => 'x', 25 => 'y', 26 => 'z', 27 => 'A', 28 => 'B',
        29 => 'C', 30 => 'D', 31 => 'E', 32 => 'F', 33 => 'G', 34 => 'H', 35 => 'I',
        36 => 'J', 37 => 'K', 38 => 'L', 39 => 'M', 40 => 'N', 41 => 'O', 42 => 'P',
        43 => 'Q', 44 => 'R', 45 => 'S', 46 => 'T', 47 => 'U', 48 => 'V', 49 => 'W',
        50 => 'X', 51 => 'Y', 52 => 'Z', 53 => '1', 54 => '2', 55 => '3', 56 => '4',
        57 => '5', 58 => '6', 59 => '7', 60 => '8', 61 => '9', 62 => '0'
    ];
    $res = "";
    for($i = 0; $i < $lenght; $i++){
        $res .= $string_arr[mt_rand(1, 62)];
    }
    return $res;
}

标签: none

添加新评论