【问题标题】:How Do I Encode a Unicode String in Base36 in PHP?如何在 PHP 中对 Base36 中的 Unicode 字符串进行编码?
【发布时间】:2011-03-14 17:24:04
【问题描述】:

我已经学会了如何在 PHP 中对字符串进行 Base16 编码,但是如何在 PHP 中对字符串进行 Base36 编码和解码?

注意我需要这个来使字符串在 URL 中工作。

奖励:如果您知道如何在执行 Base36 之前先稍微压缩字符串,那就更酷了! :)

【问题讨论】:

  • 你想在 url 中做什么!
  • 您还可以查看 URL 安全版本的 Base64 编码/解码 (here's a PHP version)

标签: php url encode base36


【解决方案1】:

我很久以前写过这篇文章,但我认为它从那以后就没有改变过;)

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

【讨论】:

  • 不幸的是,我拥有的数据是 Unicode,而不是 ASCII。但是,我应该说明这一点。
【解决方案2】:

谷歌告诉我这个:http://darklaunch.com/2009/07/31/base36-encode-and-decode-using-php-with-example-base36-encode-base36-decode

无论如何,如果您想在 URL 中使用base64,它应该可以满足您的需求。

奖励:gzcompress()gzuncompress() ;)(必须安装 Zlib 扩展)。

【讨论】:

  • 但我认为 Base64 在 URL 中创建了额外的字符,可以解释为查询参数。那不会有问题吗?这种情况下的解决办法是什么?
  • 我知道base64编码的字符串可以以一两个=(等于)结尾,但我认为如果你把它用作GET值(&var=base64_encoded_value_here==)也不是问题。我不是 100% 确定,但基于此,我相信应该。
  • Base64 使用斜线 (/) 和加号 (+)。使其在 url 中工作的最简单方法是将 / 和 + 替换为 _ 和 - (strtr(base64str, '/+', '_-'))。参见 Python 的 urlsafe_b64encode
  • base_convert() 不适用于任意长度的二进制(包括非 ASCII 符号)字符串
  • @Volomike 如果您像@mcrumley 建议的那样使用strtr( base64_encode( $input ), '/+', '_-' ),它将适用于您的unicode 字符串(或者实际上是任何二进制字符串)。然后,在另一端,使用base64_decode( strtr( $input, '_-', '/+' ) ) 检索原始的 unicode(或二进制)数据。
猜你喜欢
  • 1970-01-01
  • 2013-10-08
  • 1970-01-01
  • 2017-05-28
  • 2014-04-16
  • 2011-06-12
相关资源
最近更新 更多