note 19687 deleted from function.rawurldecode by nlopess
| From: | nlopess@php.net | Date: | Tue, 13 Jan 2004 14:47:34 +0000 |
| Subject: | note 19687 deleted from function.rawurldecode by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-63427@lists.php.net to get a copy of this message | ||
Note Submitter: ronen@greyzone.com
----
This is a unicode safe version of rawurldecode. encoded entities %unnnn n={0..F} get decoded to
matching mutlibyte utf-8 entities.
Note: this function uses utf8Encode() which I posted in the comments section for PHP function
utf8_encode ( http://www.php.net/manual/function.utf8-encode.php
).
/**
* RFC1738 compliant replacement to PHP's rawurldecode - which actually works with unicode
(using utf-8 encoding)
* @param $source [STRING]
* @return unicode safe rawurldecoded string [STRING]
* @access public
*/
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 .= utf8Encode ($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;
}