Thursday, July 23, 2015

How to create customized unique id like YouTube video id


Dear friend,
Some time we need to generate unique id for your clietns. we can use mysql generator id if we need to use that Id in printing.

for example.
you are creating bill for your client. you need to mention client id. if you will enter client id -:1, it wont look good.

so i write some code to achieve this goal.

Code 1-: 

function idGenerator1($length) {
  $random = '';
  for ($i = 0; $i < $length; $i++) {
    $random .= chr(rand(ord('a'), ord('z')));
  }
  $random .= rand(0, 1) ? rand(0, 9) : chr(rand(ord('a'), ord('z')));
  return  strtoupper($random);
}



Code 2-: 

function idGenerator2($len=10)
{
    $hex = md5("your_random_salt_here_31415" . uniqid("", true));
    $pack = pack('H*', $hex);
    $uid = base64_encode($pack);        // max 22 chars
$uid = preg_replace('/[^a-zA-Z0-9\']/', '', $uid);
$uid = strtoupper(str_replace("'", '', $uid));

    if ($len<4)
        $len=4;
    if ($len>128)
        $len=128;                       // prevent silliness, can remove

    while (strlen($uid)<$len)
        $uid = $uid . gen_uuid(22);     // append until length achieved

    return substr($uid, 0, $len);
}


Note-:    Guys, I had tested this code with entry having more than 1 lac record. i dont get any duplicate key error in mysql.  so can say, this code can be use for financial purpose.

dfdsfd




No comments:

Post a Comment