继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

一只名叫tom的猫
关注TA
已关注
手记 487
粉丝 62
获赞 330

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

}

}

?>


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP