Skip to content

Instantly share code, notes, and snippets.

@kilica
Last active January 1, 2016 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kilica/8093701 to your computer and use it in GitHub Desktop.
Save kilica/8093701 to your computer and use it in GitHub Desktop.
<?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