Home Backend Development PHP Tutorial PHP's encode64 encoding class

PHP's encode64 encoding class

Jul 25, 2016 am 08:43 AM

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. <?PHP
  2. //encode64 encoding can replace the encodeURI, encodeURIComponent, and endode functions at the same time, because the selected characters will not be encoded.
  3. class Encode64{
  4. function code($str) {
  5. $KEY = ' PaAwO65goUf7IK2vi9-xq8cFTEXLCDY1Hd3tV0ryzjbpN_BlnSs4mGRkQWMZJeuh';
  6. $a = StrToBytes($str);
  7. $len = count($a);
  8. $res = $len % 3;
  9. $s = "";$i = 2;$v = 0 ;
  10. for (; $i < $len; $i += 3) {
  11. $v = $a[$i - 2] + ($a[$i - 1] << 8) + ($ a[$i] << 16);
  12. $s .= $KEY[$v & 0x3f];
  13. $s .= $KEY[($v >> 6) & 0x3f];
  14. $s .= $KEY[($v >> 12) & 0x3f];
  15. $s .= $KEY[($v >> 18)];
  16. }
  17. if ($res == 1) {
  18. $v = $a[$i - 2];
  19. $s .= $KEY[$v & 0x3f];
  20. $s .= $KEY[($v >> 6) & 0x3f];
  21. } else if ($res == 2) {
  22. $v = $a[$i - 2] + ($a[$i - 1] << 8);
  23. $s .= $KEY[$v & 0x3f ];
  24. $s .= $KEY[($v >> 6) & 0x3f];
  25. $s .= $KEY[($v >> 12) & 0x3f];
  26. }
  27. return $s ;
  28. }
  29. function decode($codeStr) {
  30. $KEY = 'PaAwO65goUf7IK2vi9-xq8cFTEXLCDY1Hd3tV0ryzjbpN_BlnSs4mGRkQWMZJeuh';
  31. $dic = array();
  32. for ($i = 0; $i < 64; $i++) {
  33. $dic [$KEY[$i]] = $i;
  34. }
  35. $len = strlen($codeStr);
  36. $res = $len % 4;
  37. $cLen = floor($len/4)*3;
  38. if( $res==2) $clen += 1;
  39. elseif($res==3) $clen += 2;
  40. $code = range(0,$clen);
  41. $i = 3;$v = 0; $j = 0;
  42. for (; $i < $len; $i += 4) {
  43. $v = $dic[$codeStr[$i - 3]];
  44. $v += $dic[$codeStr [$i - 2]] << 6;
  45. $v += $dic[$codeStr[$i - 1]] << 12;
  46. $v += $dic[$codeStr[$i] ] << 18;
  47. $code[$j] = $v & 0xff;
  48. $code[$j+1] = ($v >> 8) & 0xff;
  49. $code[$j+2 ] = ($v >> 16) & 0xff;
  50. $j += 3;
  51. }
  52. if ($res == 2) {//The correct number of bytes must be 2 or 3, not 1 situation, if it occurs, discard it.
  53. $v = $dic[$codeStr[$i - 3]];
  54. $v += $dic[$codeStr[$i - 2]] << 6;
  55. $code [$j] = $v & 0xff;
  56. } else if ($res == 3) {
  57. $v = $dic[$codeStr[$i - 3]];
  58. $v += $dic[$codeStr[ $i - 2]] << 6;
  59. $v += $dic[$codeStr[$i - 1]] << 12;
  60. $code[$j] = $v & 0xff;
  61. $ code[$j+1] = ($v >> 8) & 0xff;
  62. }
  63. return BytesToStr($code);
  64. }
  65. }
  66. function BytesToStr($bytes) {
  67. $str = '';
  68. foreach($bytes as $ch) {
  69. $str .= chr($ch);
  70. }
  71. return iconv('UTF-16BE','utf-8',$str);
  72. }
  73. function StrToBytes($str ) {
  74. $str = iconv('utf-8','UTF-16BE',$str);
  75. $len = strlen($str);
  76. $bytes = array();
  77. for($i=0; $i<$len;$i++) {
  78. $bytes[] = ord($str[$i]) ;
  79. }
  80. return $bytes;
  81. }
  82. ?>
Copy code

php


Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles