首页 > 代码库 > 随机生成字符串,数字,手机号,邮箱

随机生成字符串,数字,手机号,邮箱

/**
* echo nRand(‘mail‘);die;
* @param
array $type
* @param
int $len
* @return
int|string
*/
function nRand($type=[],$len= 20
)
{
$t = gettype($type
);
if($t === ‘string‘
){
$rand=""
;
if($type == ‘phone‘
)
{
$rand=0
;
for ($i = 1; $i < 9; ++$i
) {
$rand .=mt_rand(0, 9
);
}
return (int) ‘13‘.$rand
;
}elseif($type === ‘mail‘
){
$rand=0
;
for ($i = 1; $i < 9; ++$i
) {
$rand .=mt_rand(0, 9
);
}
return ‘13‘.$rand.‘@‘.$rand.‘com‘
;
}else
{
for ($i = 0; $i < $len; $i
++)
{
$rand .= chr(mt_rand(33, 126
));
}
return $rand
;
}

}elseif ($t === "integer"){
$rand=0
;
$str = ‘0123456789‘
;
$max=strlen($str)-1
;
for ($i = 1; $i < $len; ++$i
) {
$rand .=$str[mt_rand(0, $max
)];
}
return $rand
;
}else
{
$rand = ‘‘
;
$str = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwsyz0123456789‘
;
$max=strlen($str)-1
;
for ($i = 1; $i <= $len; ++$i
) {
$rand .=$str[mt_rand(0, $max
)];
}
return $rand
;
}

}

随机生成字符串,数字,手机号,邮箱