Advertisement
cuonic

Generate Random String - Fixed !

Jun 18th, 2011
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.69 KB | None | 0 0
  1. <?php
  2.  
  3. function genRandomString($length = 15) {
  4.  
  5.     $validCharacters = "abcdefghijklmnopqrstuxyvwzABCDEFGHIJKLMNOPQRSTUXYVWZ";
  6.     $validCharNumber = strlen($validCharacters);
  7.  
  8.     $result = "";
  9.  
  10.     for ($i = 0; $i < $length; $i++)
  11.     {
  12.         $index = mt_rand(0, $validCharNumber - 1);
  13.             $result .= $validCharacters[$index];
  14.     }
  15.  
  16.     return $result;
  17.  
  18. }
  19.  
  20. $i = 0;
  21.  
  22. $total = 100;
  23.  
  24. while($i < $total)
  25. {
  26.     $code = genRandomString();
  27.     $length = strlen($code);
  28.    
  29.     if($length == 15)
  30.     {
  31.         echo "<span style=\"background-color: green;\">$code - $length chars</span><br/>";
  32.     }
  33.     else
  34.     {
  35.         echo "<span style=\"background-color: red;\">$code - $length chars</span><br/>";
  36.     }
  37.  
  38.     $i++;
  39. }
  40.  
  41.  
  42. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement