0

Hi,

I want to generate a unique id but stuck at one point where I need a max 10char long output like A2987F2EWS, 234EGT56GT..... unlike SHA1 or MD5 so human can read it. Code is below but the better way is accaptable by me though.

Thanks

$mysql_id                   = 1;
$mysql_id_padded            = str_pad($mysql_id, 19, '0', STR_PAD_LEFT);
$mysql_id_modified          = substr($mysql_id, -1, 1) . $mysql_id_padded;

echo $mysql_id_modified;

//Now I need to generate an id which should be user-friendly
2
Contributors
5
Replies
7
Views
5 Years
Discussion Span
Last Post by Biiim
0

sounds like you could play with this randstr function i got:

function randStr($len = 6){
    if(is_int($len) && $len > 0){
        $string = substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',$len)),0,$len);
    }else{
        $string = substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',6)),0,6);
    }
    return $string;
}

$id = randStr(10);
0

It sparked my interest so i ended up working on it myself and came up with this:

<?php
function getCodeArrays(){
    $codingArray = array(
        0=>'a'
        ,1=>'b'
        ,2=>'c'
        ,3=>'d'
        ,4=>'e'
        ,5=>'f'
        ,6=>'g'
        ,7=>'h'
        ,8=>'i'
        ,9=>'j'
    );
    $decodeArray = array();
    foreach($codingArray as $k=>$v){
        $decodeArray[$v] = $k;
    }
    $D['codeA'] = $codingArray;
    $D['decodeA'] = $decodeArray;
    return $D;
}
function encodeId($id){
    $idarray = str_split(strval($id));
    $codingArrays = getCodeArrays();
    $code = '';
    foreach($idarray as $v){
        $code .= $codingArrays['codeA'][$v];
    }
    return $code;
}
function decodeId($id){
    $idarray = str_split(strval($id));
    $codingArrays = getCodeArrays();
    $code = '';
    foreach($idarray as $v){
        $code .= $codingArrays['decodeA'][$v];
    }
    return $code;
}

$id = "0123456789";
echo encodeId($id);
echo '<br/>';
echo decodeId(encodeId($id));
echo '<br/>';
echo encodeId('52953');
echo '<br/>';
echo decodeId(encodeId('52953'));
echo '<br/>';
?>
0

Thanks Biiim.

I was actually following the steps below but I stuck after 3rd step. Do you mind giving me a hand to complete my code above please? If I use 1 for 1st step, 4th step removes last 1 when converting into number.

You may like the way that we do it. I wanted a reversible unique code that looked "random" -a fairly common problem.

1. We take an input number such as 1,942.
2. Left pad it into a string: "0000001942"
3. Put the last two digits onto the front: "4200000019"
4. Convert that into a number: 4,200,000,019

We now have a number that varies wildly between calls and is guaranteed to be less than 10,000,000,000. Not a bad start.

5. Convert that number to a Base 34 string: "2oevc0b"
6. Replace any zeros with 'y' and any ones with 'z': "2oevcyb"
7. Upshift: "2OEVCYB"

The reason for choosing base 34 is so that we don't worry about 0/O and 1/l collisions. Now you have a short random-looking key that you can use to look up a LONG database identifier.

Edited by veledrom: .

0
  1. We take an input number such as 1,942.
  2. Left pad it into a string: "0000001942"
  3. Put the last two digits onto the front: "4200000019"
  4. Convert that into a number: 4,200,000,019
  5. Convert that number to a Base 34 string: "2oevc0b"
  6. Replace any zeros with 'y' and any ones with 'z': "2oevcyb"
  7. Upshift: "2OEVCYB"

Heres how i would do the steps in php

<?php

$ids = range(1,50);
$ids[] = 1234;//some bigger numbers
$ids[] = 9432;
$ids[] = 3957432;
foreach($ids as $v){
    $steps = array();
    $padded = str_pad($v, 9, "0", STR_PAD_LEFT);//pad front with 0's
    $steps[] = $padded;
    $front = substr($padded,-2);//last 2 chars
    $steps[] = $front;
    $end = substr($padded,0,-2);//string excluding last 2 chars
    $steps[] = $end;
    $codedid = intval('1'.$front.$end);//add number on front incase of 00?
    $steps[] = $codedid;
    $basecode = base_convert($codedid, 10, 34);//convert to base 34
    $steps[] = $basecode;
    $basecode = strval($basecode);//back to string
    $steps[] = $basecode;
    $basecode = str_replace('0','y',$basecode);//0 to y
    $steps[] = $basecode;
    $basecode = str_replace('1','z',$basecode);//1 to z
    $steps[] = $basecode;
    $basecode = strtoupper($basecode);
    echo $v;
    foreach($steps as $step){
        echo ' ---> '.$step;
    }
    echo "<br/>\r\n";
}
0

also the max integar in php is 2 147 483 647 (as i just found out) - when it went over that in the above script it just set the number to 2147483647 so the code was the same for them.

you could likely work around that by splitting a number in half and appending the 2 codes together

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.