Created
December 5, 2013 16:49
-
-
Save Ciantic/7808970 to your computer and use it in GitHub Desktop.
Unique ID (alpha numeric)
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 | |
/** | |
* Unique ID alpha numeric (0-9a-z) | |
* | |
* @return 20 character alphanumeric unique id | |
**/ | |
function getUID($id=null) { | |
$s = uniqid('', true); | |
$hex = substr($s, 0, 13); | |
$dec = $s[13] . substr($s, 15); // skip the dot | |
$uid = base_convert($hex, 16, 36) . | |
($id ? base_convert($id, 10, 36) : '') . | |
base_convert($dec, 10, 36); | |
$slen = strlen($uid); | |
if ($slen > 20) | |
return substr($uid, 0, 20); | |
// Pad from right to 20 chars | |
for ($i=0; $i < 20 - $slen; $i++) { | |
$uid .= base_convert(rand(0, 35), 10, 36); | |
} | |
return $uid; | |
} |
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
/** | |
* Unique ID | |
* | |
* @return string 32 character alphanumeric ID | |
*/ | |
function uniqid() { | |
var out = new Date().getTime().toString(36).split(""), | |
outlen = out.length; | |
for (var i = 0; i < 32 - outlen; i++) | |
out.push(Math.floor(Math.random() * 36).toString(36)); | |
return out.join(""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment