php从一组数字(字符)随机选择一个数字或者一串字符

转自:http://www.blogguy.cn/show-376-1.html

第一个函数: 随机选择一串数字或者字符中的一个。

function GetID(){         
 $characters = array("99","149","150","151","145","146","147","141","142","143","137","138","139","133","134","135","129","130","131","125","126","127","96","97","98","105","106","107","108","109","110","111","112","115","116","117","118","119","120","121","122","123","103","102","100","153","154","155");   
 shuffle($characters);   
 $ReqID = $characters[mt_rand(0, count($characters))];   
 return $ReqID;   
  }         
  
  $ReqID .= GetID();   
  
  echo $ReqID;    

第二个函数:指定字符,返回指定长度的随机串

function code($nc, $a='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') {   
   $l=strlen($a)-1; $r='';   
   while($nc-->0) $r.=$a{mt_rand(0,$l)};   
   return $r;   
}   
echo(code(10));  

第三个函数:指定字符返回8位随机串

$acceptedChars = 'azertyuiopqsdfghjklmwxcvbnAZERTYUIOPQSDFGHJKLMWXCVBN0123456789';    
$max = strlen($acceptedChars)-1;   
$password = null;   
for($i=0; $i < 8; $i++) {    
 $password .= $acceptedChars{mt_rand(0, $max)};    
}   
echo $password."

";

第四个函数:指定字符,返回序列号格式的字符串

function GetID($x){         
  
    $characters = array("A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "2", "3", "4", "5", "6", "7", "8", "9");   
    shuffle($characters);   
  
    for (; strlen($ReqID)<$x;){   
      $ReqID .= $characters[mt_rand(0, count($characters))];   
    }   
  
    return $ReqID;   
       
  }         
  
  
  $ReqID .= GetID(3);   
  $ReqID .= "-";   
  $ReqID .= GetID(4);   
  $ReqID .= "-";   
  $ReqID .= GetID(3);   
  
  echo $ReqID 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据