PHP קוד:
<?php
error_reporting(E_ALL);
class generator
{
/**
* Chars array
*
* The list of the Chars of the strings
*
* @access private
* @var Char array
*/
var $tags;
/**
* Tags num
*
* The num of the Tags in the tags array
*
* @access private
* @var int
*/
var $tags_num;
/**
* code array
*
* The current array that contain the chars of the generator output
*
* @access private
* @var string
*/
var $code;
function generator( $start_length = 1 )
{
for($i=1; $i<=$start_length; $i++)
$this->code[]=0;
$this->tags=array
(
"1","2","3","4","5","6","7","8","9","0",
"a","b","c","d","e","f","g","h","i","g","k","l","m"
,"n","o","p","q","r","s","t","u","v","w","x","y","z"
);
$this->tags_num=count($this->tags);
$this->update_key=0;
}
function next_code()
{
if($this->code[$this->update_key]>=$this->tags_num-1)
{
for($i=0; $i<=$this->update_key; $i++ )
$this->code[$i]=0;
$this->update_key++;
$this->code[]=-1;
$this->next_code();
$this->update_key=0;
}
else
{
$this->code[$this->update_key]++;
}
}
function get_code()
{
$thecode = "";
foreach($this->code as $key=>$value)
{
$thecode.= ($this->tags[$value]);
}
$this->next_code();
return strrev($thecode);
}
}
$codes = new generator();
for($l=1; $l<=10; $l++)
{
echo $codes->get_code()."<br />";
}
?>
אם אתה רוצה שהקוד יתחיל ממחרוזת בגודל יותר גדול מ1 אז תעביר את הגודל הרצוי כפרמטר לפונקציה generator ככה:
PHP קוד:
$codes = new generator(4);