Home >Backend Development >PHP Tutorial >php编写批量生成不重复的卡号密码代码_php技巧

php编写批量生成不重复的卡号密码代码_php技巧

WBOY
WBOYOriginal
2016-05-16 20:15:281205browse

闲的蛋疼的时候,顺便加强下自己对PHP中数组操纵的一些技巧,就写了下面的一段小代码,可以随机生成卡号密码对应的数组,并且自动去重复,思路没有,纯粹瞎掰。

<&#63;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(); &#63;>

方法二:

<&#63;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);
&#63;>

以上所述就是本文的全部内容了,希望大家能够喜欢。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn