Skip to content

Instantly share code, notes, and snippets.

@Ciantic
Created December 5, 2013 16:49
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 Ciantic/7808970 to your computer and use it in GitHub Desktop.
Save Ciantic/7808970 to your computer and use it in GitHub Desktop.
Unique ID (alpha numeric)
<?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;
}
/**
* 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