手记

php生成一个可选位数的随机码

<?php

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'];

}

}


/**

 * 生成一个随机的数字英文混合编码

 * @param int $num 生成几位数字

 * @param int $method 可定义随机码组合方式

 * return string

 */

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'];

}

}

?>


0人推荐
随时随地看视频
慕课网APP