當前位置:才華齋>計算機>php語言>

PHP解密Unicode及Escape加密字串

php語言 閱讀(1.09W)

在json中不支援中文,用它傳送中文資料就會出現資料丟失或者亂碼,必須在傳送前對要傳送的.字串進行編碼,由於傳送過去需要用js進行資料解析,考慮 到js中有unescape函式,故若在php中有個escape函式,對資料進行編碼,在客戶端用unescape進行 解碼,這樣就會方便很多。

PHP解密Unicode及Escape加密字串

本文給大家分享一個PHP解密Unicode及Escape加密字串函式

?

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]{2,5});/', $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);

}