PHP implementation of escape and unescape function code in Javascript _ PHP Tutorial

Source: Internet
Author: User
PHP implements escape and unescape function code sharing in Javascript. PHP implementation of escape in Javascript and unescape function code sharing this article mainly introduces PHP implementation of escape and unescape function code sharing, this article provides two implementation versions, use PHP to implement the escape and unescape function code in Javascript.

This article mainly introduces how to implement the escape and unescape functions in Javascript in PHP. This article provides two implementation versions. For more information, see

This class is quite useful. what is the function? PHP uses JSON to transmit GBK characters, such as Chinese, Japanese, and Korean. Unicode is the most suitable ..

?

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

Classcoding

{

// Simulates the functions of the javascript escape and UNESCAPE functions.

Functionunescape ($ str)

{

$ Text = preg_replace_callback ("/% u [0-9A-Za-z] {4}/", array (

& $ This,

'Toutf8'

), $ Str );

Returnmb_convert_encoding ($ text, "gb2312", "UTF-8 ");

}

FunctiontoUtf8 ($ ar)

{

Foreach ($ aras $ val ){

$ Val = intval (substr ($ val, 2), 16 );

If ($ val <0x7F) {// else -007f

$ C. = chr ($ val );

} Elseif ($ val <0x800) {// 0080-0800

$ C. = chr (0xC0 | ($ val/64 ));

$ C. = chr (0x80 | ($ val % 64 ));

} Else {// 0800-FFFF

$ C. = chr (0xE0 | ($ val/64)/64 ));

$ C. = chr (0x80 | ($ val/64) % 64 ));

$ C. = chr (0x80 | ($ val % 64 ));

}

}

Return $ c;

}

Functionescape ($ string, $ encoding = 'gb2312 ')

{

$ Return = '';

For ($ x = 0; $ x

$ Str = mb_substr ($ string, $ x, 1, $ encoding );

If (strlen ($ str)> 1) {// multi-byte characters

$ Return. = '% u'. strtoupper (bin2hex (mb_convert_encoding ($ str, 'ucs-2', $ encoding )));

} Else {

$ Return. = '%'. strtoupper (bin2hex ($ str ));

}

}

Return $ return;

}

Functiongb2utf8 ($ string, $ encoding = 'utf-8', $ from_encode = 'gb2312 ')

{

Returnmb_convert_encoding ($ string, $ encoding, $ from_encode );

}

}

?>

Another similar script found on google code

?

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

85

86

87

88

89

90

91

92

93

94

95

Functionphpescape ($ str)

{

$ Sublen = strlen ($ str );

$ RetrunString = "";

For ($ I = 0; $ I <$ sublen; $ I ++)

{

If (ord ($ str [$ I]) >= 127)

{

$ TmpString = bin2hex (iconv ("gbk", "UCS-2", substr ($ str, $ I, 2 )));

$ TmpString = substr ($ tmpString, 2, 2). substr ($ tmpString, 0, 2 );

$ RetrunString. = "% u". $ tmpString;

$ I ++;

} Else {

$ RetrunString. = "%". dechex (ord ($ str [$ I]);

}

}

Return $ retrunString;

}

Functionescape ($ str)

{

Preg_match_all ("/[\ x80-\ xff]. | [\ x01-\ x7f] +/", $ str, $ r );

$ Ar = $ r [0];

Foreach ($ aras $ k => $ v)

{

If (ord ($ v [0]) <128)

$ Ar [$ k] = rawurlencode ($ v );

Else

$ Ar [$ k] = "% u". bin2hex (iconv ("UTF-8", "UCS-2", $ v ));

}

Returnjoin ("", $ ar );

}

Functionphpunescape ($ source)

{

$ DecodedStr = "";

$ Pos = 0;

$ Len = strlen ($ source );

While ($ pos <$ len)

{

$ CharAt = substr ($ source, $ pos, 1 );

If ($ charAt = '% ')

{

$ Pos ++;

$ CharAt = substr ($ source, $ pos, 1 );

If ($ charAt = 'u ')

{

// We got a unicode character

$ Pos ++;

$ UnicodeHexVal = substr ($ source, $ pos, 4 );

$ Unicode = hexdec ($ unicodeHexVal );

$ Entity = "& #". $ unicode .';';

$ DecodedStr. = utf8_encode ($ entity );

$ Pos + = 4;

} Else {

// We have an escaped ascii character

$ HexVal = substr ($ source, $ pos, 2 );

$ DecodedStr. = chr (hexdec ($ hexVal ));

$ Pos + = 2;

}

} Else {

$ DecodedStr. = $ charAt;

$ Pos ++;

}

}

Return $ decodedStr;

}

Functionunescape ($ str)

{

$ Str = rawurldecode ($ str );

Preg_match_all ("/(? : % U. {4}) | & # x. {4}; | & # \ d +; |. +/U ", $ str, $ r );

$ Ar = $ r [0];

# Print_r ($ ar );

Foreach ($ aras $ k => $ v)

{

If (substr ($ v, 0, 2) = "% u ")

$ Ar [$ k] = iconv ("UCS-2", "UTF-8", pack ("H4", substr ($ v,-4 )));

Elseif (substr ($ v, 0, 3) = "& # x ")

$ Ar [$ k] = iconv ("UCS-2", "UTF-8", pack ("H4", substr ($ v, 3,-1 )));

Elseif (substr ($ v, 0, 2) = "&#")

{

// Echo substr ($ v, 2,-1 )."";

$ Ar [$ k] = iconv ("UCS-2", "UTF-8", pack ("n", substr ($ v, 2,-1 )));

}

}

Returnjoin ("", $ ar );

}

?>

This article describes how to use PHP to implement the escape and unescape function code in Javascript. This article provides two implementation versions...

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.