note 58442 added to function.utf8-encode
| From: | Janci at osu1 dot php dot net | Date: | Fri, 04 Nov 2005 12:00:52 +0000 |
| Subject: | note 58442 added to function.utf8-encode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-98065@lists.php.net to get a copy of this message | ||
I was searching for a function similar to Javascript's unescape(). In most cases it is OK to
use url_decode() function but not if you've got UTF characters in the strings. They are
converted into %uXXXX entities that url_decode() cannot handle.
I googled the net and found a function which actualy converts these entities into HTML entities
(&#xxx;) that your browser can show correctly. If you're OK with that, the function can be
found here: http://pure-essence.net/stuff/code/utf8RawUrlDecode.phps
But it was not OK with me because I needed a string in my charset to make some comparations and
other stuff. So I have modified the above function and in conjuction with code2utf() function
mentioned in some other note here, I have managed to achieve my goal:
<?php
/**
* Function converts an Javascript escaped string back into a string with specified charset (default
is UTF-8).
* Modified function from http://pure-essence.net/stuff/code/utf8RawUrlDecode.phps
*
* @param string $source escaped with Javascript's escape() function
* @param string $iconv_to destination character set will be used as second paramether in the iconv
function. Default is UTF-8.
* @return string
*/
function unescape($source, $iconv_to = 'UTF-8') {
$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);
$decodedStr .= code2utf($unicode);
$pos += 4;
}
else {
// we have an escaped ascii character
$hexVal = substr ($source, $pos, 2);
$decodedStr .= chr (hexdec ($hexVal));
$pos += 2;
}
}
else {
$decodedStr .= $charAt;
$pos++;
}
}
if ($iconv_to != "UTF-8") {
$decodedStr = iconv("UTF-8", $iconv_to, $decodedStr);
}
return $decodedStr;
}
/**
* Function coverts number of utf char into that character.
* Function taken from: http://sk2.php.net/manual/en/function.utf8-encode.php#49336
*
* @param int $num
* @return utf8char
*/
function code2utf($num){
if($num<128)return chr($num);
if($num<2048)return chr(($num>>6)+192).chr(($num&63)+128);
if($num<65536)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 '';
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.utf8-encode.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+58442
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58442&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58442&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58442&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58442&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58442&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58442&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58442&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+58442&report=yes
Search -- http://master.php.net/manage/user-notes.php