PHP Implementation ENCODE64 Encoding class example _php tutorial

Source: Internet
Author: User

PHP implementation ENCODE64 Encoding class instance


This article mainly introduces the implementation of PHP Encode64 encoding class, examples of PHP implementation of ENCODE64 coding skills, with a certain reference value, the need for friends can refer to the next

This paper describes the PHP implementation encode64 encoding class. Share to everyone for your reference. Specific as follows:

Encode64 can obtain the shortest 26 English-size alphanumeric plus "-_" two symbols encoded data, the string can be transmitted freely in the network, without regard to the confusion caused by automatic transcoding. Cons: For large strings too slow, the reason is unclear, maybe the php script itself is slow, so it built a lot of functions, these functions if the implementation of the script is intolerable. JavaScript does not have this problem, the script is much faster.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

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,endode function at the same time

Because none of the selected characters will be encoded.

Class encode64{

function code ($STR) {

$KEY = ' Paawo65gouf7ik2vi9-xq8cftexlcdy1hd3tv0ryzjbpn_blnss4mgrkqwmzjeuh ';

$a = strtobytes ($STR);

$len = count ($a);

$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 >>) & 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 >>) & 0x3f];

}

return $s;

}

function decode ($CODESTR) {

$KEY = ' Paawo65gouf7ik2vi9-xq8cftexlcdy1hd3tv0ryzjbpn_blnss4mgrkqwmzjeuh ';

$dic = Array ();

for ($i = 0; $i < $i + +) {

$dic [$KEY [$i]] = $i;

}

$len = strlen ($CODESTR);

$res = $len% 4;

$cLen = Floor ($len/4)

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 >>) & 0xFF;

$j + = 3;

}

if ($res = = 2) {

The correct number of bytes is definitely more than 2 or 3, No 1 case, if present, discard.

$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 is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/973111.html www.bkjia.com true http://www.bkjia.com/PHPjc/973111.html techarticle PHP Implementation ENCODE64 Encoding Class example This article mainly introduces the implementation of PHP Encode64 coding class, example of PHP implementation of ENCODE64 coding skills, with a certain reference value, the need for friends can ...

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