note 119219 added to function.uniqid
| From: | php at dot dot dot metehanarslan dot com | Date: | Thu, 21 Apr 2016 00:33:16 +0000 |
| Subject: | note 119219 added to function.uniqid | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205292@lists.php.net to get a copy of this message | ||
Here is my approach to generate short string unique id. I am just increasing base of UUID. If you
need even shorter keys you may add more chars to $index . When $index gets longer $out will be
shorter.
<?php
function struuid($entropy)
{
$s=uniqid("",$entropy);
$num= hexdec(str_replace(".","",(string)$s));
$index = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$base= strlen($index);
$out = '';
for($t = floor(log10($num) / log10($base)); $t >= 0; $t--) {
$a = floor($num / pow($base,$t));
$out = $out.substr($index,$a,1);
$num = $num-($a*pow($base,$t));
}
return $out;
}
echo struuid(false); //Return sample: F4518NTQTQ
echo struuid(true); //Return sample: F451FAHSUCD90N6YNRBQHLZ9E1W
?>
----
Server IP: 72.52.91.14
Probable Submitter: 88.245.243.72
----
Manual Page -- http://php.net/manual/en/function.uniqid.php
Edit -- https://master.php.net/note/edit/119219
Del: integrated -- https://master.php.net/note/delete/119219/integrated
Del: useless -- https://master.php.net/note/delete/119219/useless
Del: bad code -- https://master.php.net/note/delete/119219/bad+code
Del: spam -- https://master.php.net/note/delete/119219/spam
Del: non-english -- https://master.php.net/note/delete/119219/non-english
Del: in docs -- https://master.php.net/note/delete/119219/in+docs
Del: other reasons-- https://master.php.net/note/delete/119219
Reject -- https://master.php.net/note/reject/119219
Search -- https://master.php.net/manage/user-notes.php