PHP随机生成不重复的8位卡号(数字)和卡密(字符串)

php教程11个月前 (01-28)2011

一、生成不重复的随机数字,可自定义长度

/**

 * 生成不重复的随机数字

 * @param  int $start  需要生成的数字开始范围

 * @param  int $end    结束范围

 * @param  int $length 需要生成的随机数个数

 * @return number      生成的随机数

 */

function getRandNumber($start=0,$end=9,$length=8){

//初始化变量为0

$connt = 0;

//建一个新数组

$temp = array();

while($connt < $length){

//在一定范围内随机生成一个数放入数组中

$temp[] = mt_rand($start, $end);

//$data = array_unique($temp);

//去除数组中的重复值用了“翻翻法”,就是用array_flip()把数组的key和value交换两次。这种做法比用 array_unique() 快得多。

$data = array_flip(array_flip($temp));

//将数组的数量存入变量count中

$connt = count($data);

}

//为数组赋予新的键名

shuffle($data);

//数组转字符串

$str=implode(",", $data);

//替换掉逗号

$number=str_replace(',', '', $str);

return $number;

}

二、随机生成不重复的8位卡密

function makeCardPassword() {

        $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

        $rand = $code[rand(0,25)]

            .strtoupper(dechex(date('m')))

            .date('d').substr(time(),-5)

            .substr(microtime(),2,5)

            .sprintf('%02d',rand(0,99));

        for(

            $a = md5( $rand, true ),

            $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',

            $d = '',

            $f = 0;

            $f < 8;

            $g = ord( $a[ $f ] ),

            $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],

            $f++

        );

        return  $d;

}


标签: 随机卡号

“PHP随机生成不重复的8位卡号(数字)和卡密(字符串)” 的相关文章

一些恶搞小程序~嘿嘿~10年前 (2014-04-08)
mysql中的unsigned10年前 (2014-05-22)
优化mysql之key_buffer_size10年前 (2014-05-29)
linux下vi编辑器命令大全10年前 (2014-06-09)

评论列表

dmca
9个月前 (03-08)

学习了,涨姿势啊!!

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。