PHP随机⽣成不重复的8位卡号(数字)和卡密(字符串)⼀、⽣成不重复的随机数字,可⾃定义长度(最多⽀持10位数)
1/**
2 * ⽣成不重复的随机数字(不能超过10位数,否则while循环陷⼊死循环)
3 * @param int $start 需要⽣成的数字开始范围
4 * @param int $end 结束范围
5 * @param int $length 需要⽣成的随机数个数
6 * @return number ⽣成的随机数
7*/
8function getRandNumber($start = 0, $end = 9, $length = 8)
9 {
10//初始化变量为0
11$count = 0;
12//建⼀个新数组
13$temp = array();
14while ($count < $length) {
15//在⼀定范围内随机⽣成⼀个数放⼊数组中
16$temp[] = mt_rand($start, $end);
17//$data = array_unique($temp);
18 //去除数组中的重复值⽤了“翻翻法”,就是⽤array_flip()把数组的key和value交换两次。这种做法⽐⽤ array_unique() 快得多。
19$data = array_flip(array_flip($temp));
20//将数组的数量存⼊变量count中
21$count = count($data);
22 }
23//为数组赋予新的键名
24shuffle($data);
25//数组转字符串
26$str = implode(",", $data);
27//替换掉逗号
28$number = str_replace(',', '', $str);
29return$number;
30 }
31
php 数组字符串转数组32echo getRandNumber(0, 9, 8)."<br/>";
⼆、随机⽣成不重复的8位卡密
1//随机⽣成不重复的8位卡密
2function makeCardPassword()
3 {
4$code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
5$rand = $code[rand(0, 25)]
6 . strtoupper(dechex(date('m')))
7 . date('d') . substr(time(), -5)
8 . substr(microtime(), 2, 5)
9 . sprintf('%02d', rand(0, 99));
10for (
11$a = md5($rand, true),
12$s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
13$d = '',
14$f = 0;
15$f < 8;
16$g = ord($a[$f]),
17$d .= $s[($g ^ ord($a[$f + 8])) - $g & 0x1F],
18$f++
19 ) ;
20return$d;
21 }
22
23echo makeCardPassword();
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论