閑的蛋疼的時候,順便加強下自己對PHP中數組操縱的一些技巧,就寫了下面的一段小代碼,可以隨機生成卡號密碼對應的數組,并且自動去重復,思路沒有,純粹瞎掰。
<?php header('Content-Type:text/html; charset=utf-8');
function MakeCard()
{
set_time_limit(0);
//處理緩沖區
ob_end_clean();
ob_implicit_flush(true);
echo str_pad(" ", 256);
if(intval($_POST['num']>0)) $num=intval($_POST['num']); //數量
if(intval($_POST['point']>0)) $point=intval($_POST['point']); //點數
if(intval($_POST['batch']>0)) $batch=intval($_POST['batch']); //批號
if(($_POST['ym']!="")) $ym=$_POST['ym']; //發行年月
else $ym=date('ym');
if($num==0) return;
$num=$num*100; //卡的張數,即記錄數
echo "<p>開始 ".date("H:i:s")." ";
for($i=1;$i<=$num;$i++)
{
$sn=sprintf("%02s%s%06s",$batch,$ym,$i);
$seek=mt_rand(0,9999).mt_rand(0,9999).mt_rand(0,9999); //12位
$start=mt_rand(0,20);
$str=strtoupper(substr(md5($seek),$start,12));
$str=str_replace("O",chr(mt_rand(65,78)),$str);
$str=str_replace("0",chr(mt_rand(65,78)),$str);
$row=array('sn'=>$sn,'password'=>$str,'created'=>time(),'point'=>$point);
//查重
//在這里加插入數據的代碼.
print_r($row);
}
echo " 結束 ".date("H:i:s")."";
printf("<br>成功生成:%s萬個 %s點 的密碼</p>",$num/1e4,$point);
return $num;
} //函數結束
$_POST['num']=1;
$_POST['point']=10;
$_POST['batch']=10;
$_POST['ym']='1405';
echo MakeCard(); ?>
方法二:
<?php
$numLen=16;
$pwdLen=10;
$c=100;//生成100組卡號密碼
$sNumArr=range(0,9);
$sPwdArr=array_merge($sNumArr,range('A','Z'));
$cards=array();
for($x=0;$x< $c;$x++){
$tempNumStr=array();
for($i=0;$i< $numLen;$i++){
$tempNumStr[]=array_rand($sNumArr);
}
$tempPwdStr=array();
for($i=0;$i< $pwdLen;$i++){
$tempPwdStr[]=$sPwdArr[array_rand($sPwdArr)];
}
$cards[$x]['no']=implode('',$tempNumStr);
$cards[$x]['pwd']=implode('',$tempPwdStr);
}
array_unique($cards);
print_r($cards);
?>
以上所述就是本文的全部內容了,希望大家能夠喜歡。
新聞熱點
疑難解答