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.
BONU开发者_StackOverflowS: 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;
}
精彩评论