Php encode64 encoding class

Source: Internet
Author: User
Php encode64 encoding class

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.

  1. // Encode64 encoding can replace encodeURI, encodeURIComponent, and endode functions at the same time, because the selected characters are not encoded.
  2. Class Encode64 {
  3. Function code ($ str ){
  4. $ KEY = 'paawo65gouf7ik2vi9-xq8cFTEXLCDY1Hd3tV0ryzjbpN_BlnSs4mGRkQWMZJeuh ';
  5. $ A = StrToBytes ($ str );
  6. $ Len = count ($ );
  7. $ Res = $ len % 3;
  8. $ S = ""; $ I = 2; $ v = 0;
  9. For (; $ I <$ len; $ I + = 3 ){
  10. $ V = $ a [$ I-2] + ($ a [$ I-1] <8) + ($ a [$ I] <16 );
  11. $ S. = $ KEY [$ v & 0x3f];
  12. $ S. = $ KEY [($ v> 6) & 0x3f];
  13. $ S. = $ KEY [($ v> 12) & 0x3f];
  14. $ S. = $ KEY [($ v> 18)];
  15. }
  16. If ($ res = 1 ){
  17. $ V = $ a [$ I-2];
  18. $ S. = $ KEY [$ v & 0x3f];
  19. $ S. = $ KEY [($ v> 6) & 0x3f];
  20. } Else if ($ res = 2 ){
  21. $ V = $ a [$ I-2] + ($ a [$ I-1] <8 );
  22. $ S. = $ KEY [$ v & 0x3f];
  23. $ S. = $ KEY [($ v> 6) & 0x3f];
  24. $ S. = $ KEY [($ v> 12) & 0x3f];
  25. }
  26. Return $ s;
  27. }
  28. Function decode ($ codeStr ){
  29. $ KEY = 'paawo65gouf7ik2vi9-xq8cFTEXLCDY1Hd3tV0ryzjbpN_BlnSs4mGRkQWMZJeuh ';
  30. $ Dic = array ();
  31. For ($ I = 0; $ I <64; $ I ++ ){
  32. $ Dic [$ KEY [$ I] = $ I;
  33. }
  34. $ Len = strlen ($ codeStr );
  35. $ Res = $ len % 4;
  36. $ CLen = floor ($ len/4) * 3;
  37. If ($ res = 2) $ clen + = 1;
  38. Elseif ($ res = 3) $ clen + = 2;
  39. $ Code = range (0, $ clen );
  40. $ I = 3; $ v = 0; $ j = 0;
  41. For (; $ I <$ len; $ I + = 4 ){
  42. $ V = $ dic [$ codeStr [$ I-3];
  43. $ V + = $ dic [$ codeStr [$ I-2] <6;
  44. $ V + = $ dic [$ codeStr [$ I-1] <12;
  45. $ V + = $ dic [$ codeStr [$ I] <18;
  46. $ Code [$ j] = $ v & 0xff;
  47. $ Code [$ j + 1] = ($ v> 8) & 0xff;
  48. $ Code [$ j + 2] = ($ v> 16) & 0xff;
  49. $ J + = 3;
  50. }
  51. If ($ res = 2) {// The correct number of bytes must be the remainder of 2 or 3. if there is no 1, discard it.
  52. $ V = $ dic [$ codeStr [$ I-3];
  53. $ V + = $ dic [$ codeStr [$ I-2] <6;
  54. $ Code [$ j] = $ v & 0xff;
  55. } Else if ($ res = 3 ){
  56. $ V = $ dic [$ codeStr [$ I-3];
  57. $ V + = $ dic [$ codeStr [$ I-2] <6;
  58. $ V + = $ dic [$ codeStr [$ I-1] <12;
  59. $ Code [$ j] = $ v & 0xff;
  60. $ Code [$ j + 1] = ($ v> 8) & 0xff;
  61. }
  62. Return BytesToStr ($ code );
  63. }
  64. }
  65. Function BytesToStr ($ bytes ){
  66. $ Str = '';
  67. Foreach ($ bytes as $ ch ){
  68. $ Str. = chr ($ ch );
  69. }
  70. Return iconv ('utf-16be', 'utf-8', $ str );
  71. }
  72. Function StrToBytes ($ str ){
  73. $ Str = iconv ('utf-8', 'utf-16BE ', $ str );
  74. $ Len = strlen ($ str );
  75. $ Bytes = array ();
  76. For ($ I = 0; $ I <$ len; $ I ++ ){
  77. $ Bytes [] = ord ($ str [$ I]);
  78. }
  79. Return $ bytes;
  80. }
  81. ?>


Php

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.