Php implementation of the encode64 encoding class _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php implements the encode64 encoding class instance. This article describes how to implement the encode64 encoding class in php. The example shows how to implement the encode64 encoding class in php, which has some reference value, if you need a friend, you can use php to implement the encode64 encoding class instance.

This article mainly introduces the php encode64 encoding class. The example analyzes php's skills for implementing encode64 encoding, which has some reference value. if you need it, you can refer

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.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

// Encode64 encoding can replace encodeURI, encodeURIComponent, and endode functions 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 ($ );

$ 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> 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-xq8cFTEXLCDY1Hd3tV0ryzjbpN_BlnSs4mGRkQWMZJeuh ';

$ 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;

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> 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.

This article describes how to implement the encode64 encoding class in php. The example shows how to implement the encode64 encoding in php. it has some reference value...

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.