首页 > 代码库 > PHP生成随机字符串

PHP生成随机字符串

<?php

 class RandChar{

  function getRandChar($length){
   $str = null;
   $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
   $max = strlen($strPol)-1;

   for($i=0;$i<$length;$i++){
    $str.=$strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
   }

   return $str;
  }
 }

 $randCharObj = new RandChar();
 echo $randCharObj->getRandChar(5);
?>

80个随机字符串生成

<?php
class RandChar{

  function getRandChar($length){
   $str = null;
   $strPol = "0123456789abcdefghijklmnopqrstuvwxyz";
   $max = strlen($strPol)-1;

   for($i=0;$i<$length;$i++){
    $str.=$strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
   }

   return $str;
  }
 }

 $randCharObj = new RandChar();
$word=array($randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5));
$word1=array($randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5));
$word2=array($randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5));
$word3=array($randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5));
$word4=array($randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5));
$word5=array($randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5));
$word6=array($randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5));
$word7=array($randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5),$randCharObj->getRandChar(5));
print_r($word);echo "<br />";
print_r($word1);echo "<br />";
print_r($word2);echo "<br />";
print_r($word3);echo "<br />";
print_r($word4);echo "<br />";
print_r($word5);echo "<br />";
print_r($word6);echo "<br />";
print_r($word7);echo "<br />";

?>

 

PHP生成随机字符串