본문 바로가기

PHP

(함수) shuffle() & str_shuffle() - 랜덤함수

랜덤1 : shuffle()


function random_password_1($cnt)  
{  
    $word_en = "a,b,c,d,e,f,g,h,i,j,k,l,m,o,p,q,r,s,t,u,v,w,x,y,z";  
    $word_no = "1,2,3,4,5,6,7,8,9,0";  
    $word = $word_en.",".$word_no;
    $array=explode(",",$word);  
    shuffle($array);  
    $newstring = implode($array,""); 
    return substr($newstring, 0, $cnt);  
}

echo random_password_1('5');


랜덤2 : str_shuffle()


function random_password_2($cnt) {
   $word  = 'abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
   return substr(str_shuffle($word), 0, $cnt);
}
echo random_password_2('5');