note 62707 added to function.urldecode
| From: | Aardvark at osu1 dot php dot net | Date: | Tue, 07 Mar 2006 21:20:46 +0000 |
| Subject: | note 62707 added to function.urldecode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-105014@lists.php.net to get a copy of this message | ||
The function below can be used to convert a query parameter resulting from applying the JavaScript
escape function to a Unicode string back to Unicode. The function was modified from a previously
published function to handle escaped ASCII values in the range 128-255 which are converted to
standard (and not Unicode) escapes by the escape function. The option parameter allows an
altenative encoding to UTF-8 to be apploed. (More and related info can be found at http://www.kanolife.com/escape/).
function code2utf($num){
if($num<128)
return chr($num);
if($num<1024)
return chr(($num>>6)+192).chr(($num&63)+128);
if($num<32768)
return chr(($num>>12)+224).chr((($num>>6)&63)+128)
.chr(($num&63)+128);
if($num<2097152)
return chr(($num>>18)+240).chr((($num>>12)&63)+128)
.chr((($num>>6)&63)+128).chr(($num&63)+128);
return '';
}
function unescape($strIn, $iconv_to = 'UTF-8') {
$strOut = '';
$iPos = 0;
$len = strlen ($strIn);
while ($iPos < $len) {
$charAt = substr ($strIn, $iPos, 1);
if ($charAt == '%') {
$iPos++;
$charAt = substr ($strIn, $iPos, 1);
if ($charAt == 'u') {
// Unicode character
$iPos++;
$unicodeHexVal = substr ($strIn, $iPos, 4);
$unicode = hexdec ($unicodeHexVal);
$strOut .= code2utf($unicode);
$iPos += 4;
}
else {
// Escaped ascii character
$hexVal = substr ($strIn, $iPos, 2);
if (hexdec($hexVal) > 127) {
// Convert to Unicode
$strOut .= code2utf(hexdec ($hexVal));
}
else {
$strOut .= chr (hexdec ($hexVal));
}
$iPos += 2;
}
}
else {
$strOut .= $charAt;
$iPos++;
}
}
if ($iconv_to != "UTF-8") {
$strOut = iconv("UTF-8", $iconv_to, $strOut);
}
return $strOut;
}
----
Server IP: 85.116.0.130
Probable Submitter: 82.43.105.192
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.urldecode.php
Edit -- http://master.php.net/note/edit/62707
Del: integrated -- http://master.php.net/note/delete/62707/integrated
Del: useless -- http://master.php.net/note/delete/62707/useless
Del: bad code -- http://master.php.net/note/delete/62707/bad+code
Del: spam -- http://master.php.net/note/delete/62707/spam
Del: non-english -- http://master.php.net/note/delete/62707/non-english
Del: in docs -- http://master.php.net/note/delete/62707/in+docs
Del: other reasons-- http://master.php.net/note/delete/62707
Reject -- http://master.php.net/note/reject/62707
Search -- http://master.php.net/manage/user-notes.php