Home >Backend Development >PHP Tutorial >PHP decrypts Unicode and Escape encrypted strings_PHP tutorial
Chinese is not supported in json. If you use it to transmit Chinese data, data will be lost or garbled. You must send it before sending it. The string is encoded. Since the transmission requires js for data parsing, considering that there is an unescape function in js, if there is an escape function in php, the data is encoded and unescape is used to decode it on the client. This will Much more convenient.
This article will share with you a PHP decryption Unicode and Escape encrypted string function
?
|
<🎜>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 u8FD9u662Fu6D4Bu8BD5u6587u672CuFF01'; echo uni_decode($str); // Hello everyone, I am a lonely soul! This is test text! |
Search on the Internet, there are many escape functions implemented in php, they are similar
?
2 3 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; } |