PHP 生成随机字符串

一、生成18位随机字符串

function getOrderNo()
{
    $out_trade_no = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 10);
    return $out_trade_no;

}

二、生成指定长度的随机字符串

function getStr($num = 32)
{
    $str_num = '0123456789';
    $str_low = 'abcdefghijklmnopqrstuvwxyz';
    $str_upp = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

    $str = '';
    for($i=0;$i<$num;$i++) {
        $r = rand(0,2);

        if($r == 0) $shuffle = str_shuffle($str_num);
        if($r == 1) $shuffle = str_shuffle($str_low);
        if($r == 2) $shuffle = str_shuffle($str_upp);

        $length = mb_strlen($shuffle);
        $str .= substr($shuffle, -1, $length-1);
    }
    return $str;
}

你可能感兴趣的:(PHP)