Last active
January 1, 2016 04:39
-
-
Save kilica/8093701 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$list = new RandomStringList(1000, 4); | |
$list->output(); | |
class RandomStringList | |
{ | |
// remove l,I,O,0,1 | |
const CHARACTERS = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"; | |
protected $number = 0; | |
protected $length = 0; | |
protected $list = array(); | |
public function __construct($number = 100, $length=4) | |
{ | |
$this->number = $number; | |
$this->length = $length; | |
for($i=0; $i<$this->number; $i++){ | |
$this->list[] = $this->getString(); | |
} | |
} | |
public function output() | |
{ | |
echo implode(',', $this->list); | |
} | |
public function createString() | |
{ | |
$num = strlen(self::CHARACTERS); | |
$result = ''; | |
for($i=0; $i<$this->length; $i++){ | |
$result .= substr(self::CHARACTERS, mt_rand(0, $num-1), 1); | |
} | |
return $result; | |
} | |
protected function getString() | |
{ | |
$string = $this->createString($this->length); | |
// check duplicated item in list | |
if(in_array($string, $this->list)){ | |
$string = $this->getString(); | |
} | |
return $string; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment