php 正確解碼javascript中通過escape編碼後的字元

來源:互聯網
上載者:User

這是很久以前收集的一個,不知道誰寫的了,但經過測試沒有問題~
JavaScript代碼 複製代碼 代碼如下:function phpUnescape($escstr)
{
preg_match_all("/%u[0-9A-Za-z]{4}|%.{2}|[0-9a-zA-Z.+-_]+/", $escstr, $matches);
$ar = &$matches[0];
$c = "";
foreach($ar as $val)
{
if (substr($val, 0, 1) != "%")
{
$c .= $val;
} elseif (substr($val, 1, 1) != "u")
{
$x = hexdec(substr($val, 1, 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編碼後: 複製代碼 代碼如下:%u6D4B%u8BD5www.jb51.net%22%22%27%27%3C%3E%26%26

解碼後: 複製代碼 代碼如下:測試www.jb51.net""''<>&&

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.