This article describes the PHP implementation encode64 encoding class. Share to everyone for your reference. Specifically as follows:
Encode64 can obtain the shortest number of 26 English uppercase and lowercase letters plus "-_" two symbols encoded data, this string can be transmitted freely on the network, without regard to the confusion caused by automatic transcoding. Disadvantage: For the large string is too slow, the reason is unknown, the PHP script may itself is slow, so it has a number of functions, these functions if the implementation of the foot is intolerable. JavaScript does not have this problem, the script is much faster.
?
PHP//encode64 encoding can be substituted for the Encodeuri,encodeuricomponent,endode function//Because the selected characters are not encoded.
Class encode64{function code ($STR) {$KEY = ' paawo65gouf7ik2vi9-xq8cftexlcdy1hd3tv0ryzjbpn_blnss4mgrkqwmzjeuh ';
$a = strtobytes ($STR);
$len = count ($a);
$res = $len% 3;
$s = ""; $i = 2; $v = 0;
for (; $i < $len; $i + + 3) {$v = $a [$i-2] + ($a [$i-1] << 8) + ($a [$i] << 16);
$s. = $KEY [$v & 0x3f];
$s. = $KEY [($v >> 6) & 0x3f];
$s. = $KEY [($v >>) & 0x3f];
$s. = $KEY [($v >> 18)];
} if ($res = = 1) {$v = $a [$i-2];
$s. = $KEY [$v & 0x3f];
$s. = $KEY [($v >> 6) & 0x3f];
else if ($res = = 2) {$v = $a [$i-2] + ($a [$i-1] << 8);
$s. = $KEY [$v & 0x3f];
$s. = $KEY [($v >> 6) & 0x3f];
$s. = $KEY [($v >>) & 0x3f];
return $s;
function decode ($codeStr) {$KEY = ' paawo65gouf7ik2vi9-xq8cftexlcdy1hd3tv0ryzjbpn_blnss4mgrkqwmzjeuh ';
$dic = Array (); For ($i = 0; $i < 64;
$i + +) {$dic [$KEY [$i]] = $i;
} $len = strlen ($CODESTR);
$res = $len% 4;
$cLen = Floor ($len/4) *3;
if ($res ==2) $clen + + 1;
ElseIf ($res ==3) $clen + + 2;
$code = Range (0, $clen); $i = 3; $v = 0;
$j = 0;
for (; $i < $len; $i + + 4) {$v = $dic [$CODESTR [$i-3]];
$v + + $dic [$CODESTR [$i-2]] << 6;
$v + + $dic [$CODESTR [$i-1]] << 12;
$v + + $dic [$CODESTR [$i]] << 18;
$code [$j] = $v & 0xFF;
$code [$j +1] = ($v >> 8) & 0xFF;
$code [$j +2] = ($v >>) & 0xFF;
$j + 3;
} if ($res = = 2) {//The correct number of bytes must be more than 2 or 3, No 1 case, if present, discard.
$v = $dic [$CODESTR [$i-3]];
$v + + $dic [$CODESTR [$i-2]] << 6;
$code [$j] = $v & 0xFF;
else if ($res = = 3) {$v = $dic [$CODESTR [$i-3]];
$v + + $dic [$CODESTR [$i-2]] << 6;
$v + + $dic [$CODESTR [$i-1]] << 12;
$code [$j] = $v & 0xFF;
$code [$j +1] = ($v >> 8) & 0xFF;
Return Bytestostr ($code); }} function BytEstostr ($bytes) {$str = ';
foreach ($bytes as $ch) {$str. = Chr ($ch);
Return Iconv (' utf-16be ', ' utf-8 ', $str);
function Strtobytes ($str) {$str = Iconv (' utf-8 ', ' utf-16be ', $str);
$len = strlen ($STR);
$bytes = Array ();
for ($i =0; $i < $len; $i + +) {$bytes [] = Ord ($str [$i]);
return $bytes; }?>
I hope this article will help you with your PHP programming.