ExeQue

PHP Random String + StringArray Generator

Oct 1st, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.55 KB | None | 0 0
  1. <?php
  2.  
  3. class Tools {
  4.  
  5.     /**
  6.      * Generates a random string of a variable length and charset
  7.      * @param int $length
  8.      * Length of the returned string
  9.      * @param string $chars
  10.      * Charset
  11.      * @return string
  12.      * Random string of the set length using the set charset
  13.      */
  14.  
  15.     public static function GenerateRandomString($length = 10, $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890') {
  16.         $cCount = strlen($chars);
  17.         $r = '';
  18.         for ($i = 0; $i < $length; $i++) {
  19.             $r .= $chars[rand(0, $cCount - 1)];
  20.         }
  21.         return $r;
  22.     }
  23.  
  24.  
  25.     /**
  26.      * Generates an array of random strings (no doubles) with a variable count, length and charset
  27.      * @param $count
  28.      * Amount of entries in the returned array
  29.      * @param $length
  30.      * Length of the returned strings
  31.      * @param string $chars
  32.      * Charset
  33.      * @return array|bool
  34.      * Array on success - False on failure (requested a higher count than possible with the set charset and string length)
  35.      */
  36.     public static function GenerateRandomStringArray($count, $length, $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890') {
  37.         if(pow(strlen($chars), $length) >= $count){
  38.             $arr = array();
  39.             $i = 0;
  40.             while($i < $count) {
  41.                 $str = Tools::GenerateRandomString($length, $chars);
  42.                 if(!in_array($str, $arr)) {
  43.                     $arr[] = $str;
  44.                     $i++;
  45.                 }
  46.             }
  47.             return $arr;
  48.         }
  49.         else return false;
  50.     }
  51. }
  52.  
  53. ?>
  54.  
  55. Tools::GenerateRandomStringArray(10, 16) =>
  56. array(10) {
  57.     [0]=> string(16) "eCegsdzjJqhjxky9"
  58.     [1]=> string(16) "Qv9686e3lCykXekM"
  59.     [2]=> string(16) "WvgLLwB7pU3akJOM"
  60.     [3]=> string(16) "vAChnzHwXhKRGO18"
  61.     [4]=> string(16) "wLcDXdvhXLGoZvWN"
  62.     [5]=> string(16) "23YQwr3orkPLhMzO"
  63.     [6]=> string(16) "ZoZT3giPoZcZxEWb"
  64.     [7]=> string(16) "qRdMwGgCOKM4K81T"
  65.     [8]=> string(16) "jpEwYHWLHBAXWbOX"
  66.     [9]=> string(16) "G4k7pjle4xCH7Pkm"
  67. }
  68.  
  69. Tools::GenerateRandomStringArray(10, 16, "01") =>
  70. array(10) {
  71.     [0]=> string(16) "0001001010011111"
  72.     [1]=> string(16) "0001110100010100"
  73.     [2]=> string(16) "0000110011001110"
  74.     [3]=> string(16) "0100010100100011"
  75.     [4]=> string(16) "0001110010010101"
  76.     [5]=> string(16) "1001000111011110"
  77.     [6]=> string(16) "1000111000111110"
  78.     [7]=> string(16) "0111110010111100"
  79.     [8]=> string(16) "1100010100100010"
  80.     [9]=> string(16) "1100000011010001"
  81. }
Add Comment
Please, Sign In to add comment