在json中不支持中文,用它傳送中文數據就會出現數據丟失或者亂碼,必須在傳送前對要發送的.字符串進行編碼,由於傳送過去需要用js進行數據解析,考慮 到js中有unescape函數,故若在php中有個escape函數,對數據進行編碼,在客户端用unescape進行 解碼,這樣就會方便很多。
本文給大家分享一個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);
}