PHP生成随机字符串

Posted lyg0126

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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生成随机字符串的主要内容,如果未能解决你的问题,请参考以下文章

21个常用代码片段

php随机密码函数的实例代码

PHP生成随机字符串与唯一字符串

PHP 生成随机字符串与唯一字符串

php生成随机字符串函数

php中如何生成1-15之间的随机数?