php 正确解码javascript中通过escape编码后的字符

前端之家收集整理的这篇文章主要介绍了php 正确解码javascript中通过escape编码后的字符前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这是很久以前收集的一个,不知道谁写的了,但经过测试没有问题~
JavaScript代码
<div class="codetitle"><a style="CURSOR: pointer" data="88911" class="copybut" id="copybut88911" onclick="doCopy('code88911')"> 代码如下:

<div class="codebody" id="code88911">
function PHPUnescape($escstr)
{
preg_matchall("/%u[0-9A-Za-z]{4}|%.{2}|[0-9a-zA-Z.+-]+/",$escstr,$matches);
$ar = &$matches[0];
$c = "";
foreach($ar as $val)
{
if (substr($val,1) != "%")
{
$c .= $val;
} elseif (substr($val,1,1) != "u")
{
$x = hexdec(substr($val,2));
$c .= chr($x);
}
else
{
$val = intval(substr($val,2),16);
if ($val < 0x7F) // 0000-007F
{
$c .= chr($val);
} elseif ($val < 0x800) // 0080-0800
{
$c .= chr(0xC0 | ($val / 64));
$c .= chr(0x80 | ($val % 64));
}
else // 0800-FFFF
{
$c .= chr(0xE0 | (($val / 64) / 64));
$c .= chr(0x80 | (($val / 64) % 64));
$c .= chr(0x80 | ($val % 64));
}
}
}
return $c;
}

escape编码后:
<div class="codetitle"><a style="CURSOR: pointer" data="80625" class="copybut" id="copybut80625" onclick="doCopy('code80625')"> 代码如下:
<div class="codebody" id="code80625">
%u6D4B%u8BD5www.jb51.cc%22%22%27%27%3C%3E%26%26

解码后:
<div class="codetitle"><a style="CURSOR: pointer" data="58598" class="copybut" id="copybut58598" onclick="doCopy('code58598')"> 代码如下:
<div class="codebody" id="code58598">
测试www.jb51.cc""''<>&&

原文链接:https://www.f2er.com/php/28820.html
escapejavascriptphp

猜你在找的PHP相关文章