PHP中进行unicode编码后,在JavaScript中如何解码?js中&#104这样的,怎么转码?

php代码示例

https://www.jb51.net/article/1.htm字符串被编码成:&#104&#116&#116&#112&#115&#58&#47&#47&#119&#119&#119&#46&#106&#98&#53&#49&#46&#110&#101&#116&#47&#97&#114&#116&#105&#99&#108&#101&#47&#49&#46&#104&#116&#109

$str='https://www.jb51.net/article/1.htm';
echo UnicodeEncode($str);
echo unicodeDecode(UnicodeEncode($str));
function UnicodeEncode($str){ //编码
    preg_match_all('/./u',$str,$matches);
    $unicodeStr = "";
    foreach($matches[0] as $m){
        $unicodeStr .= "&#".base_convert(bin2hex(iconv('UTF-8',"UCS-4",$m)),16,10);
    }
    return $unicodeStr;
}
function unicodeDecode($unicode_str){ //解码
    $json = '{"str":"'.$unicode_str.'"}';
    $arr = json_decode($json,true);
    if(empty($arr)) return '';
    return $arr['str'];
}

JavaScript中如何解码示例

浏览器控制台输出https://www.jb51.net/article/1.htm

function unt(str) {
	return str.replace(/&#(x)?([^&]{1,5});?/g, function (a, b, c) {
		return String.fromCharCode(parseInt(c, b ? 16 : 10));
	})
}
//带;号
var str="https://www.jb51.net/article/1.htm";
//不带分号
var str2="&#104&#116&#116&#112&#115&#58&#47&#47&#119&#119&#119&#46&#106&#98&#53&#49&#46&#110&#101&#116&#47&#97&#114&#116&#105&#99&#108&#101&#47&#49&#46&#104&#116&#109";
console.log(unt(str));
console.log(unt(str2));