2

How can I use base_convert to map numbers to letters?

for example 123456 would become ABCDEF. So a number 123321 would become ABCCBA

I actually have a unique number, that needs to keep it's uniqueness in the form of a string of letters. Any way to do this?

gray
  • 798
  • 18
  • 31

1 Answers1

8

base_convert() is for converting numbers between different number systems. Like Hex to Bin.

For your task is strtr():

$original = '13421';
$replaced = strtr($original, '12345', 'ABCDE');
echo $replaced; // output: ACDBA

As you see strtr works like a char-wise translator. If a char out of the $from string is found in the input string, it will be translated by the char that is at the same position of the $to string. However, it's better explained by the code example above :)

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • what do you mean with any length of number? – hek2mgl May 09 '13 at 20:48
  • 2
    @gray strtr stands for string translate. essentially it translates one set of characters to another by replacing each letter in the second argument (`12345`) to the letter in the same spot in the third argument (`ABCDE`). – Jonathan Kuhn May 09 '13 at 20:51