php 解决 escape 编码后 js 解码中午输出乱码的问题

版权声明:APan的博客 https://blog.csdn.net/qq_40729514/article/details/85446926

我们通过 php  进行 escape  编码 加密我们的文本后  通过js 的unescape 进行解码,然而 中文出现乱码,主要是因为 现在网络上所在流传的 PHP escape  编码方法存在 一些问题 ,下面的方法是我个人修改后的 ,提供参考

function phpescape($str){//这个是加密用的
    preg_match_all("/[\xc2-\xdf][\x80-\xbf]+|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}|[\x01-\x7f]+/e",$str,$newstr);
    $ar = $newstr[0];
    foreach($ar as $k=>$v){
        if(ord($ar[$k])>=127){
            $tmpString=bin2hex(iconv("utf-8","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;
}

猜你喜欢

转载自blog.csdn.net/qq_40729514/article/details/85446926