note 71211 added to function.urldecode
| From: | pedanticathotmailcojp at osu1 dot php dot net | Date: | Thu, 16 Nov 2006 16:22:58 +0000 |
| Subject: | note 71211 added to function.urldecode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-119810@lists.php.net to get a copy of this message | ||
The following function will decode %uXXXX
sequentially, without temporary data.
<?php
function decode_unicode_url($str)
{
$res = '';
$i = 0;
$max = strlen($str) - 6;
while ($i <= $max)
{
$character = $str[$i];
if ($character == '%' && $str[$i + 1] == 'u')
{
$value = hexdec(substr($str, $i + 2, 4));
$i += 6;
if ($value < 0x0080) // 1 byte: 0xxxxxxx
$character = chr($value);
else if ($value < 0x0800) // 2 bytes: 110xxxxx 10xxxxxx
$character =
chr((($value & 0x07c0) >> 6) | 0xc0)
. chr(($value & 0x3f) | 0x80);
else // 3 bytes: 1110xxxx 10xxxxxx 10xxxxxx
$character =
chr((($value & 0xf000) >> 12) | 0xe0)
. chr((($value & 0x0fc0) >> 6) | 0x80)
. chr(($value & 0x3f) | 0x80);
}
else
$i++;
$res .= $character;
}
return $res . substr($str, $i);
}
?>
Simple test with japanese characters,
combined with urldecode:
<?php
$str = decode_unicode_url('%u65E5%u672C%u8A9E');
print(mb_convert_encoding(urldecode($str), "sjis", "euc-jp, utf-8, sjis") .
'<br/>');
?>
----
Server IP: 194.246.101.61
Probable Submitter: 81.56.13.114
----
Manual Page -- http://www.php.net/manual/en/function.urldecode.php
Edit -- https://master.php.net/note/edit/71211
Del: integrated -- https://master.php.net/note/delete/71211/integrated
Del: useless -- https://master.php.net/note/delete/71211/useless
Del: bad code -- https://master.php.net/note/delete/71211/bad+code
Del: spam -- https://master.php.net/note/delete/71211/spam
Del: non-english -- https://master.php.net/note/delete/71211/non-english
Del: in docs -- https://master.php.net/note/delete/71211/in+docs
Del: other reasons-- https://master.php.net/note/delete/71211
Reject -- https://master.php.net/note/reject/71211
Search -- https://master.php.net/manage/user-notes.php