bianbian coding life

便便代码人生: 关注技术, 翻译文档, 偶尔动动手

[原]php的base64解码函数

Posted by bianbian on 2007-03-19 04:22

本文Tags: ,

调用 base64_decode() 就行了。
我承认我很BT地觉得是不是php的这个函数有问题,导致我原先提到的问题产生:php处理base64编码和Unicode客户端交互的问题
于是我照着某文档,重新实现了一下。结果发现是一样的(废话)。-___-!!
我承认我错了,当然前面那个问题不是php的原因。不过既然已经写了,我就贴一下吧。至少base64解码俺是会了。哈哈,对了,还有C语言实现的base64解码,下次也来贴一下。

  1. //php功能等同于base64_decode(),所以没有任何实际用途
  2. function my_base64_decode(&$str)
  3. {
  4.     $Bstr_base64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  5.     $len = strlen($str);
  6.     if ($len % 4 !=0)
  7.         return '';
  8.     $res = $bins = '';
  9.     for ($i = 0; $i < $len; $i++)
  10.     {
  11.         $nChar = substr($str, $i, 1);
  12.         if ($nChar == '=')
  13.             break;
  14.         $oldValue = strpos($Bstr_base64, $nChar);
  15.         $binValue = substr('000000' . decbin($oldValue), -6);
  16.         $bins .= $binValue;
  17.        
  18.         if (strlen($bins) >= 8)
  19.         {
  20.             $deChar = substr($bins, 0, 8);
  21.             $bins = substr($bins, 8);
  22.             $res .= chr(bindec($deChar));
  23.         }
  24.     }
  25.    
  26.     return $res;
  27. }
标签: ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

3 Responses to “[原]php的base64解码函数”

  1. You XU Says:

    我想请教一下您是用什么方法贴代码以及用什么程序看php代码的呢?
    我最近要读php 代码,没找到顺手的工具。

  2. bianbian Says:

    你是说贴到blog里吗?我用的是WP的插件coolcode。
    编写php代码我老土了,用的是UltraEdit。不过听说有专门的Zend Studio,还有PHP Edit之类的。不过我已经习惯UltraEdit了,呵呵。

  3. Maxwin Says:

    net最近写这么多php的文章

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

(required)