批量生成大小写英文字母和数字混合的19位字符串
要完成需求其实也并不难,该方法是基于递归的形式实现,代码如下:
# $arr 为结果数组 $len 代表要生成多少位的字符
function createKey( $arr = [] , $len = 19 ){
if( count( $arr ) == 100 ){
return $arr;
}
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklnmopqrstuvwxyz0123456789';
$str_len = strlen( $str );
$new_str = '';
while ( $len ){
$rand = rand( 0 , $str_len -1 );
$new_str .= $str[$rand];
$len --;
}
$arr[$new_str] = rand( 0 , 10);
return $this -> createKey( $arr , 19 );
}
如果觉得不错的话,赞赏一下吧?