5/16/2004 ↓

Javascript escape encode to PHP readable

Author: Mark Ghosh Category: Cool Scripts

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;
}

1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 4 out of 5)
Loading ... Loading ...

Visitors who read this post, also read:

    Friends

    Translate

    Translate to German Translate to Spanish Translate to French Translate to Italian Translate to Portuguese Translate to Japanese Translate to Korean Translate to Russian Translate to Chinese

    Latest Videos

    4 Comments | Leave a comment | Comments RSS

    1. This is a test comment

      [Reply] Mark — 10/21/2004 @ 6:06 pm
    2. [...] 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 am
    3. The 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 pm
    4. You’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

    Leave a comment

    Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    (required)

    (required, will not be published)


    S2