HuyPV
Wednesday, January 9, 2013
function unescape($source) {
$decodedStr = "";
$pos = 0;
$len = strlen($source);
while ($pos < $len) {
$charAt = substr($source, $pos, 1);
if ($charAt == '%') {
$pos++;
$charAt = substr($source, $pos, 1);
if ($charAt == 'u') {
// we got a unicode character
$pos++;
$unicodeHexVal = substr($source, $pos, 4);
$unicode = hexdec($unicodeHexVal);
$entity = "&#" . $unicode . ';';
$decodedStr .= utf8_encode($entity);
$pos += 4;
} else {
// we have an escaped ascii character
$hexVal = substr($source, $pos, 2);
$unicode = hexdec($hexVal);
$entity = "&#" . $unicode . ';';
$decodedStr .= utf8_encode($entity);
$pos += 2;
}
} else {
$decodedStr .= $charAt;
$pos++;
}
}
return mb_convert_encoding($decodedStr, 'UTF-8', 'HTML-ENTITIES');
}
function escape($str = 'Phùng Văn Huy <> ~!#$%^&(){}[]=:,;?\'"\\') {
$xChars = '<> ~!#$%^&(){}[]=:,;?\'"\\';
# Danh dau la xu ly xau utf-8
mb_internal_encoding('utf-8');
$len = mb_strlen($str);
$ret = '';
for ($i = 0; $i < $len; $i++) {
$uniChar = mb_substr($str, $i, 1);
$uniCharCode = uniord($uniChar);
if ($uniCharCode < 32 || $uniCharCode > 127 || strpos($xChars, $uniChar) !== false) {
$hex = strtoupper(dechex($uniCharCode));
if (strlen($hex) < 3) {
$ret .= '%' . $hex;
} else {
$ret .= '%u' . str_pad($hex, 4, '0', STR_PAD_LEFT);
}
} else {
$ret .= $uniChar;
}
}
return $ret;
}
function uniord($char) {
$utf8Character = $char;
list(, $ord) = unpack('N', mb_convert_encoding($utf8Character, 'UCS-4BE', 'UTF-8'));
return $ord;
}
function unichr($u) {
return mb_convert_encoding('&#' . intval($u) . ';', 'UTF-8', 'HTML-ENTITIES');
}
Title:
PHP unescape JS escape string
Description:
function unescape($source) { $decodedStr = ""; $pos = 0; $len = strlen($source); while ($pos < $len) { $charAt = s...
...
Rating:
4