PHP decrypts Unicode and Escape encrypted strings

Source: Internet
Author: User

PHP decrypts Unicode and Escape encrypted strings

Json does not support Chinese characters. If you use json to transmit Chinese data, data loss or garbled characters may occur. The strings to be sent must be encoded before transmission, in the past, JavaScript was used for data parsing. Considering that there is a scape function in js, if there is an escape Function in php, the data will be encoded and decoded on the client using unescape, this will be much more convenient.

This article will share with you a PHP function used to decrypt Unicode and Escape encrypted strings.

?

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

<? Php

Function uni_decode ($ s ){

Preg_match_all ('/\ # ([0-9] {}) \;/', $ s, $ html_uni );

Preg_match_all ('/[\\\%] u ([0-9a-f] {4})/ie', $ s, $ js_uni );

$ Source = array_merge ($ html_uni [0], $ js_uni [0]);

$ Js = array ();

For ($ I = 0; $ I <count ($ js_uni [1]); $ I ++ ){

$ Js [] = hexdec ($ js_uni [1] [$ I]);

}

$ Utf8 = array_merge ($ html_uni [1], $ js );

$ Code = $ s;

For ($ j = 0; $ j <count ($ utf8); $ j ++ ){

$ Code = str_replace ($ source [$ j], unicode2utf8 ($ utf8 [$ j]), $ code );

}

Return $ code; // $ s; // preg_replace ('// \\\ u ([0-9a-f] {4})/ie ', "chr (hexdec ('\ 1')", $ s );

}

 

Function unicode2utf8 ($ c ){

$ Str = "";

If ($ c <0x80 ){

$ Str. = chr ($ c );

} Else if ($ c <0x800 ){

$ Str. = chr (0xc0 | $ c> 6 );

$ Str. = chr (0x80 | $ c & 0x3f );

} Else if ($ c <0x10000 ){

$ Str. = chr (0xe0 | $ c> 12 );

$ Str. = chr (0x80 | $ c> 6 & 0x3f );

$ Str. = chr (0x80 | $ c & 0x3f );

} Else if ($ c <0x200000 ){

$ Str. = chr (0xf0 | $ c> 18 );

$ Str. = chr (0x80 | $ c> 12 & 0x3f );

$ Str. = chr (0x80 | $ c> 6 & 0x3f );

$ Str. = chr (0x80 | $ c & 0x3f );

}

Return $ str;

}

 

$ Str = '% u5927 % u5BB6 % u597D % uFF0C I am a lone soul! <Br/> \ u8FD9 \ u662F \ u6D4B \ u8BD5 \ u6587 \ u672C \ uFF01 ';

Echo uni_decode ($ str); // Hello, everyone! This is the test text!

Search for one on the Internet. Many escape functions implemented using php are similar.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Function phpescape ($ str ){

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

$ Ar = $ newstr [0];

Foreach ($ ar as $ k => $ v ){

If (ord ($ ar [$ k]) & gt; = 127 ){

$ TmpString = bin2hex (iconv ("GBK", "ucs-2", $ v ));

If (! Eregi ("WIN", PHP_ OS )){

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

}

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

} Else {

$ ReString. = rawurlencode ($ v );

}

}

Return $ reString;

}

The above is all the content of this article. I hope you will like it.

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.