1. 程式人生 > >PHP成隨機字串

PHP成隨機字串


生成隨機字串


/**
 * 隨機字串
 * @param int $len
 * @return string
 */
function randomStr($len = 32)
{
    $chars = "abcdefghijklmnopqrstuvwxyz";
    $shuffle = str_shuffle($chars);
    $result = '';
    for ($i=0;$i<$len;$i++)
    {
        $index = mt_rand(0,strlen($chars));
        $result .= substr($shuffle,$index,1);
    }
    return $result;
}


生成隨機數


/**
 * 生成隨機數
 * @param int $len
 * @return string
 */
function randomNum($len = 6)
{
    $arr = range(0,9);
    $result = '';
    for ($i=0;$i<$len;$i++)
    {
        $result .= $arr[mt_rand(0,count($arr)-1)];
    }

    return $result;
}