Tips for using ENCODE64 encoding classes in PHP

Source: Internet
Author: User
This article mainly introduces the implementation of PHP Encode64 encoding class, examples of PHP implementation of ENCODE64 coding skills, with a certain reference value, the need for friends can refer to the next

This paper describes the PHP implementation encode64 encoding class. Specific as follows:

Encode64 can obtain the shortest 26 English-size alphanumeric plus "-_" two symbols encoded data, the string can be transmitted freely in the network, without regard to the confusion caused by automatic transcoding. Cons: For large strings too slow, the reason is unclear, maybe the php script itself is slow, so it built a lot of functions, these functions if the implementation of the script is intolerable. JavaScript does not have this problem, the script is much faster.

<? PHP//ENCODE64 encoding can replace the Encodeuri,encodeuricomponent,endode function at the same time//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 < $i + +) {$dic[$KEY [$i]] = $i;  } $len = strlen ($CODESTR);  $res = $len% 4;  $cLen = Floor ($len/4)  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 is definitely 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;}? >

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related Article

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.