Php implements the encode64 encoding class instance and encode64 Encoding

Source: Internet
Author: User

Php implements the encode64 encoding class instance and encode64 Encoding

This example describes how to implement the encode64 encoding class in php. Share it with you for your reference. The details are as follows:

Encode64 can obtain the minimum data encoded by 26 uppercase/lowercase letters and numbers plus the "-_" symbol. This string can be freely transmitted over the network, there is no need to consider the confusion caused by automatic transcoding. disadvantage: It is too slow for a large string for unknown reasons. Maybe the PHP script itself is slow, so it has many built-in functions, which are intolerable if implemented using scripts. javaScript does not have this problem, and the script speed is much faster.

<? PHP // encode64 encoding can replace encodeURI, encodeURIComponent, and endode functions at the same time. // The selected characters are not encoded. class Encode64 {function code ($ str) {$ KEY = 'paawo65gouf7ik2vi9-callback'; $ a = StrToBytes ($ str); $ len = count ($ ); $ 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) & 0x 3f]; $ s. = $ KEY [($ v> 12) & 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> 12) & 0x3f];} return $ s;} function decode ($ codeStr) {$ KEY = 'paawo65gouf7ik2vi9-Example KQWMZJeuh '; $ 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; (; $ 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> 16) & 0xff; $ j + = 3;} if ($ res = 2) {// the correct number of bytes must be 2 or 3. If no value is 1, discard it. $ 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 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.