Home  >  Article  >  Backend Development  >  PHP's encode64 encoding class

PHP's encode64 encoding class

WBOY
WBOYOriginal
2016-07-25 08:43:281083browse

encode64 can obtain the shortest data encoded by 26 English uppercase and lowercase letters and numbers plus the two symbols "-_". This string can be transmitted freely on the network without considering the confusion caused by automatic transcoding. Disadvantages: For Large strings are too slow. The reason is unknown. Maybe the PHP script itself is slow, so it has many built-in functions. If these functions are implemented in scripts, it would be intolerable. JavaScript does not have this problem, and scripts are much faster.

  1. //encode64 encoding can replace the encodeURI, encodeURIComponent, and endode functions at the same time, because the selected characters will not be encoded.
  2. class Encode64{
  3. function code($str) {
  4. $KEY = ' PaAwO65goUf7IK2vi9-xq8cFTEXLCDY1Hd3tV0ryzjbpN_BlnSs4mGRkQWMZJeuh';
  5. $a = StrToBytes($str);
  6. $len = count($a);
  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 2 or 3, not 1 situation, if it occurs, 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. ?>
Copy code

php


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn