views:

47

answers:

2

I have learned how to Base16 encode a string in PHP, but how do I Base36 encode and decode a string in PHP?

Note I need this to make the string work in URLs.

BONUS: And if you know how to compress the string slightly first before doing the Base36, that would be even cooler! :)

A: 

I wrote this a very long time ago but I assume it hasn't changed since ;)

function number2ascii($input='', $base=10){                                                                                                                                                                                                                        
    $length = strlen($input);                                                                                                                                                                                                                                      
    $dec = base_convert(255, 10, $base);                                                                                                                                                                                                                           
    $chars = strlen($dec);                                                                                                                                                                                                                                         
    $output = '';                                                                                                                                                                                                                                                  

    for($i=0; $i<$length; $i+=$chars){                                                                                                                                                                                                                             
        $text = substr($input, $i, $chars);                                                                                                                                                                                                                        
        $dec = base_convert($text, $base, 10);                                                                                                                                                                                                                     
        $output .= chr($dec);                                                                                                                                                                                                                                      
    }                                                                                                                                                                                                                                                              
    return $output;                                                                                                                                                                                                                                                
}                                                                                                                                                                                                                                                                  


function ascii2number($input='', $base=10){                                                                                                                                                                                                                        
    $length = strlen($input);                                                                                                                                                                                                                                      
    $dec = base_convert(255, 10, $base);                                                                                                                                                                                                                           
    $chars = strlen($dec);                                                                                                                                                                                                                                         
    $output = '';                                                                                                                                                                                                                                                  

    for($i=0; $i<$length; $i++){                                                                                                                                                                                                                                   
        $dec = ord($input[$i]);                                                                                                                                                                                                                                    
        $number = base_convert($dec, 10, $base);                                                                                                                                                                                                                   
        $number = str_pad($number, $chars, 0, STR_PAD_LEFT);                                                                                                                                                                                                       
        $output .= $number.' ';                                                                                                                                                                                                                                    
    }                                                                                                                                                                                                                                                              
    return $output;                                                                                                                                                                                                                                                
}
WoLpH
Unfortunately the data I have is Unicode, not ASCII. I should have stated that, however.
Volomike
+1  A: 

Google told me this: http://darklaunch.com/2009/07/31/base36-encode-and-decode-using-php-with-example-base36-encode-base36-decode

Anyway, base64 should fit your needs if you want to use it inside an URL.

Bonus: gzcompress() and gzuncompress() ;) (Zlib extension must be installed).

Savageman
But I thought that Base64 creates extra characters in the URL that could be construed as query parameters. Wouldn't that be a problem? What's the resolution in such a case?
Volomike
Savageman
Base64 uses slashes (/) and plus (+). The easiest way to make it work in a url is to replace / and + with _ and - (`strtr(base64str, '/+', '_-')`). See Python's urlsafe_b64encode
mcrumley