在json中不支持中文,用它傳送中文數(shù)據(jù)就會(huì)出現(xiàn)數(shù)據(jù)丟失或者亂碼,必須在傳送前對(duì)要發(fā)送的字符串進(jìn)行編碼,由于傳送過去需要用js進(jìn)行數(shù)據(jù)解析,考慮 到j(luò)s中有unescape函數(shù),故若在php中有個(gè)escape函數(shù),對(duì)數(shù)據(jù)進(jìn)行編碼,在客戶端用unescape進(jìn)行 解碼,這樣就會(huì)方便很多。
本文給大家分享一個(gè)PHP解密Unicode及Escape加密字符串函數(shù)
?
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); $co在網(wǎng)上搜索一把,很多用php實(shí)現(xiàn)的escape函數(shù),大同小異
?
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])>=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; }新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注