php生成一個可選位數的隨機碼

dunne21發表於2021-09-09

echo coding(6);

function coding($num){

$str_arr = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9');

$rand_s = array_rand($str_arr,$num);

$code_s = '';

if(is_array($rand_s)){

foreach($rand_s as $v){

$code_s .= $str_arr[$v];

}

return $code_s;

}else{

return $str_arr['rand_s'];

}

}


function coding2($num,$method = 6){

$lower_arr = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');

$upper_arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');

$num_arr = array('0','1','2','3','4','5','6','7','8','9');

switch($method){

case 1: $str_arr = $num_arr;//純數字

case 2: $str_arr = $lower_arr;//純字母小寫

case 3: $str_arr = $upper_arr;//純字母大寫

case 4: $str_arr = array_merge($lower_arr,$num_arr);//字母(小寫)數字

case 5: $str_arr = array_merge($upper_arr,$num_arr);//字母(大寫)數字

case 6: $str_arr = array_merge($lower_arr,$upper_arr,$num_arr);//字母(小大寫)數字

case 7: $str_arr = array_merge($lower_arr,$upper_arr);//字母(小大寫)

}

$rand_s = array_rand($str_arr,$num);

$code_s = '';

if(is_array($rand_s)){

foreach($rand_s as $v){

$code_s .= $str_arr[$v];

}

return $code_s;

}else{

return $str_arr['rand_s'];

}

}

?>


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/1600/viewspace-2809532/,如需轉載,請註明出處,否則將追究法律責任。

相關文章