A class to generate 99.5% unqiue strings. I found that there is only one or two characters common between two subsequent strings.
<?php
class Local_RandomString {
protected $_length;
protected $_prevRand;
public function __construct($length = 15) {
$this->_length = $length;
}
public function getRand() {
$randStr = null;
$args[] = 'N' . $this->_length;
for($i = 0; $i < $this->_length; $i++) {
$args[] = mt_rand();
}
$randStr = substr(base64_encode((call_user_func_array('pack', $args))), 1, $this->_length);
$this->_prevRand = $randStr;
return $randStr;
}
public function setLength($l) {
$this->_length = (int) $l;
if($this->_length <= 0) {
throw new Exception('Invalid random string length');
}
}
public function getPrevRand() {
return $this->_prevRand;
}
}
?>