I18N
|
|
|
|
<?php /**
* RFC1738 compliant replacement to PHP's rawurldecode - which actually works with unicode (using utf-8 encoding)
* @author Ronen Botzer
* @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;
}
/**
* The following is actually a javascript method I had to write to replace escape().
* escape() is the javascript method equivalent to PHP's rawurlencode(), however it doesn't encode utf-8 multibyte characters correctly in most browsers.
*/
// Warning: the following code is JavaScript. Ugh. :D
// use this instead of escape() to ensure a correctly encoded url string (RFC1738)
function urlEncode (str) {
len = str.length;
res = new String();
charOrd = new Number();
for (i = 0; i < len; i++) {
charOrd = str.charCodeAt(i);
if ((charOrd >= 65 && charOrd <= 90) || (charOrd >= 97 && charOrd <= 122) || (charOrd >= 48 && charOrd <= 57) || (charOrd == 33) || (charOrd == 36) || (charOrd == 95)) {
// this is alphanumeric or $-_.+!*'(), which according to RFC1738 we don't escape
res += str.charAt(i);
}
else {
res += '%';
if (charOrd > 255) res += 'u';
hexValStr = charOrd.toString(16);
if ((hexValStr.length) % 2 == 1) hexValStr = '0' + hexValStr;
res += hexValStr;
}
}
return res;
}
?>
|
|
|
Usage Example
|
|
|
Rate This Script
|
|
|
|