PHP implementation ENCODE64 Code class instance _php skill

Source: Internet
Author: User
Tags php programming php script

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.