1. 程式人生 > >php 獲取隨機字串

php 獲取隨機字串

/**
 * 隨機字串
 * @param $lenth
 * @return string
 */
public function randStr($lenth){
    $chars    = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    $password = '';
    for ($i = 0; $i < $lenth; $i++) {
        $password .= $chars[mt_rand(0, strlen($chars) - 1)];
    }
    return $password;
}

//呼叫

public function index(){

       $str = $this->randStr(30);

       dd($str);

}