Jump to content

PHP Base36 Encoding/Decoding for Text/Data


hknight

Recommended Posts

I want functions similar to base64_encode() and base64_decode() however only numbers and lowercase letters should be used.

 

I want to be able to convert a string of text or binary data into a series of numbers and lowercase letters.

 

My function works with numbers but not with text or binary data.

 

<?php

echo base36_encode('555555555');
echo '<hr />';
echo base36_encode('Hello World, this is a test!');

function base36_encode($base10){
    return base_convert($base10,10,36);
}

function base36_decode($base36){
    return base_convert($base36,36,10);
}

?>

base 36 isn't a multiple of 2 which complicates things.  It might be easier to use base 32, eg a-z and 0-5.  Then you can encode exactly 5 bits of data in each character.

 

Another alternative would be to start with base 64 encoding, and then do a conversion like this:

 

a => a

b => b

...

z => z

0 => 0

1 => 1

...

8 => 8

 

So everything from a-z and 0-8 is left unchanged, but the number 9 is reserved as a special character to indicate something from the other half of the base 64 character set.  That other half can be encoded as 9 followed by one of a-z and 0-8.  eg

 

9 => 99

A => 9a

B => 9b

C => 9c

...

 

What you'll end up with is an encoding using only 36 characters.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.