Javascript escape encode to PHP readable
Thanks for visiting! We would like to serve you better. Please subscribe to our RSS feed for daily updates. This blog posts regular Wordpress news, updates of themes, plugins, ideas, hacks, quick fixes and everything about blogging, especially about Wordpress. You can also receive updates from this blog via email if you want that method of notification.
Code fragment:
$string =”%u4E2D%u56FD%u5E7B%u60F3%u6587%u5B66%u57FA%u573″;
$new_string = utf8RawUrlDecode( $string);
echo “encoded string is “.$new_string;
function utf8RawUrlDecode ($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);
$decodedStr .= chr (hexdec ($hexVal));
$pos += 2;
}
}
else {
$decodedStr .= $charAt;
$pos++;
}
}
return $decodedStr;
}


(5 votes, average: 4 out of 5)











Comments RSS
This is a test comment
[Reply] Mark — 10/21/2004 @ 6:06 pm[...] et caught as spam (though some items will get caught). This new version of the plugin uses a function that acts like the Javascript unescape comm [...]
Weblog Tools Collection » Updated Three Strikes Spam Protection Plugin Version 1.1 Beta — 10/22/2004 @ 3:09 amThe function above convert an “escaped” Unicode string, for example from a query string parameter to HTML Unicode entities. If instead you want to get a Unicode string in UTF-8 or other encodings, the function at this URL will do the trick.
[Reply] Aardvark (1 comments.) — 03/9/2006 @ 7:54 pmYou’re awesome! I’ve been fighting with this for hours. I’ve been using encodeURI() in javascript. It doesn’t fix &. That’s really annoying. Good work.
[Reply] Sam Soffes (1 comments.) — 06/27/2007 @ 10:37 pm