post-page

Javascript escape encode to PHP readable

5
responses
by
 
on
May 16th, 2004
in
Cool Scripts

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

heading
heading
5
Responses

 

Comments

  1. Mark says:

    This is a test comment

  2. Aardvark says:

    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.

  3. Sam Soffes says:

    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.



Trackbacks/Pingbacks

  1. […] ??:Javascript escape encode to PHP readable Leave a comment | Trackback No comments yet. […]

Obviously Powered by WordPress. © 2003-2013

css.php