note 71706 added to function.urlencode

From: Date: Tue, 12 Dec 2006 18:49:36 +0000
Subject: note 71706 added to function.urlencode
Groups: php.notes 
Request: Send a blank email to php-notes+get-120555@lists.php.net to get a copy of this message
In AJAX era you might need to use UCS-2 (UTF-16) url-encoding (chars represented in form '%uXXXX' - e.g. '%u043e' for Russian 'o'). But PHP is weak in working with multibyte encoded strings, so you cannot simply use urlencode() for the string in UCS-2. Here is simple function serving for this purpose. Note, that this function takes UTF8-encoded string as input and, then, for internal purposes use some 1-byte encoding (cp1251 in my case). If you have the string in some 1-byte encoding, you may remove the first iconv() and modify the second one and thus slightly simplify the function. function utf16urlencode($str) { $str = iconv("utf-8", "cp1251", $str); $res = ""; for ($i = 0; $i < strlen($str); $i++) { $res .= "%u"; $a = iconv("cp1251", "ucs-2", $str[$i]); for ($j = 0; $j < strlen($a); $j++) { $n = dechex(ord($a[$j])); if (strlen($n) == 1) { $n = "0$n"; } $res .= $n; } } return $res; } ---- Server IP: 195.222.164.18 Probable Submitter: 85.140.237.122 ---- Manual Page -- http://www.php.net/manual/en/function.urlencode.php Edit -- https://master.php.net/note/edit/71706 Del: integrated -- https://master.php.net/note/delete/71706/integrated Del: useless -- https://master.php.net/note/delete/71706/useless Del: bad code -- https://master.php.net/note/delete/71706/bad+code Del: spam -- https://master.php.net/note/delete/71706/spam Del: non-english -- https://master.php.net/note/delete/71706/non-english Del: in docs -- https://master.php.net/note/delete/71706/in+docs Del: other reasons-- https://master.php.net/note/delete/71706 Reject -- https://master.php.net/note/reject/71706 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#120555) next »