Created
November 15, 2018 04:01
-
-
Save cisiqo/9e09d3f0ea698097eb3690e1539d94f9 to your computer and use it in GitHub Desktop.
php unescape
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function unescape($str) | |
{ | |
$ret = ''; | |
$len = strlen($str); | |
for ($i = 0; $i < $len; $i ++) | |
{ | |
if ($str[$i] == '%' && $str[$i + 1] == 'u') | |
{ | |
$val = hexdec(substr($str, $i + 2, 4)); | |
if ($val < 0x7f) | |
$ret .= chr($val); | |
else | |
if ($val < 0x800) | |
$ret .= chr(0xc0 | ($val >> 6)) . | |
chr(0x80 | ($val & 0x3f)); | |
else | |
$ret .= chr(0xe0 | ($val >> 12)) . | |
chr(0x80 | (($val >> 6) & 0x3f)) . | |
chr(0x80 | ($val & 0x3f)); | |
$i += 5; | |
} else | |
if ($str[$i] == '%') | |
{ | |
$ret .= urldecode(substr($str, $i, 3)); | |
$i += 2; | |
} else | |
$ret .= $str[$i]; | |
} | |
return $ret; | |
} | |
echo unescape('A%u7ec4'). PHP_EOL; | |
function replace_unicode_escape_sequence($match) { | |
return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE'); | |
} | |
function unicode_decode($str) { | |
return preg_replace_callback('/[\\\\|%]u([0-9a-f]{4})/i', 'replace_unicode_escape_sequence', $str); | |
} | |
echo unicode_decode('A%u7ec4') . PHP_EOL; | |
echo unicode_decode('A\u7ec4') . PHP_EOL; | |
echo unicode_decode(utf8_decode('A\u7ec4')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment