1

I have this id taken from a table:

"B0AL02087"

How do I convert this alphanumeric id to a intergre number representation? I am expecting "B0AL02087" to something like "2011202087".

I found a way of doing this using an alphabet range with a string split, the index of the letter in the alphabet switches out the letters in the key:

$alphabet = range('A', 'Z');
$arr = str_split("B0AL02087");
$arr[0] = array_search($arr[0], $alphabet);
$arr[2] = array_search($arr[2], $alphabet);
$arr[3] = array_search($arr[3], $alphabet);
implode($arr), '0');

But this method only works when you specify the index of $arr which means it doesn't work when the format of the id from the table changes, e.g.

"BBAL02087"

Now we have two letters at the start of a string rather than a letter and an intergre like the first one. I could write a bunch of if statements check if each index contains a number. I just wasn't sure if there is a better way to do this.

Simon
  • 1,095
  • 2
  • 11
  • 29
  • Does converting it to binary work? It would be a complete numeric representation, and reversible.. https://stackoverflow.com/questions/6382738/convert-string-to-binary-then-back-again-using-php – Kaylined Apr 16 '18 at 16:48
  • in lieu of the void specification for ***the number***, maybe state the purpose so people dont have to shoot darts blindfolded. – YvesLeBorg Apr 16 '18 at 16:58
  • You could use `ord()` to get the ascii integer value of each letter. – Nic3500 Apr 16 '18 at 17:00
  • For what purpose do you want to convert this to a number? – Sammitch Apr 16 '18 at 17:51
  • The ultimate solution for this requirement, is to convert number from base 36 to decimal (base 10). this will void collision. – SaidbakR May 15 '18 at 17:51

2 Answers2

4

Not really sure what you are trying to achieve, but using the examples you provided, you can simply use :

str_replace(range('A', 'Z'), range(1, 26), Syour_id)

B0AL02087 will return: 2011202087

BBAL02087 will return: 2211202087

mrbm
  • 2,164
  • 12
  • 11
  • You can remove one of the range calls it you want. `str_replace(($range = range('A', 'Z')), array_keys($range), Syour_id);` – ArtisticPhoenix Apr 16 '18 at 17:05
  • 1
    Thought of that but it will replace `A` with `0`, not really handy for numerical representations, eg `A320` and `AAA320` will both have a representation equals to `320`, using the `range (1, 26)`, the representations will be `1320` and `111320` respectively. – mrbm Apr 16 '18 at 17:09
  • @mrbm there is a little limitation in this solution, suppose the following two inputs: `AAA320` and `A11320` here two different values will return the same id i.e collision is occurred! – SaidbakR May 15 '18 at 17:48
0

As I have regarded in a previous comment, the ultimate way to perform this conversion is to convert from base to base, to void collision.

From this article which is talking about converting numbers from base to another, and from the description of converting from any base to decimal base 10. I have implemented solution using that convert what it is supposed base 36, i.e. 0-9 A-Z, number to base 10 decimal which it could be easily converted to code:

baseStr = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  function baseToDec(val){
    n = val.length;
    b = baseStr.length
    s = 0;
    for (i =0; i < val.length; i++){
      n--;
      s = s + (baseStr.indexOf(val[i])*(Math.pow(b,n))); 
        // The non numerical values, such as E or Q could
       // not be multiplied, so we used its index in the baseStr
    }
    return s;
  }

This is a jsbin demo and this an online service that do such conversation to approve, in some way, that code works fine since it gives the same results of the demo.

Update

The following is the PHP implementation:

<?php
  function baseToDec($val){
    $baseStr = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $n = strlen($val);
    $b = strlen($baseStr);
    $s = 0;
   // var_dump($n);
    for ($i =0; $i < strlen($val); $i++){
      $n--;
      $s = $s + (strpos($baseStr,$val[$i])*($b**$n)); 
        // The non numerical values, such as E or Q could
       // not be multiplied, so we used its index in the $baseStr
    }
    return $s;
  }
  echo baseToDec('A11320');

ideone demo.

SaidbakR
  • 13,303
  • 20
  • 101
  • 195