简体   繁体   中英

How Do I Encode a Unicode String in Base36 in PHP?

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! :)

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).

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;                                                                                                                                                                                                                                                
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM