note 82483 added to function.urlencode
| From: | simon dot rupf at int-ag dot ch | Date: | Sat, 12 Apr 2008 22:20:19 +0000 |
| Subject: | note 82483 added to function.urlencode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-138246@lists.php.net to get a copy of this message | ||
Kerdster's function works like a charm. It has only a minor beauty flaw in my humble opinion:
It encodes every character, even the plain ascii ones. This just doesn't like nice in the
browsers address bar. ;-)
Inspired by Mkaganer's utf8_urldecode example in urldecode comments here's the enhanced
code:
<?php
function utf16_urlencode ( $str ) {
# convert characters > 255 into HTML entities
$convmap = array( 0xFF, 0x2FFFF, 0, 0xFFFF );
$str = mb_encode_numericentity( $str, $convmap, "UTF-8");
# escape HTML entities, so they are not urlencoded
$str = preg_replace( '/&#([0-9a-fA-F]{2,5});/i', 'mark\\1mark', $str
);
$str = urlencode($str);
# now convert escaped entities into unicode url syntax
$str = preg_replace( '/mark([0-9a-fA-F]{2,5})mark/i', '%u\\1', $str );
return $str;
}
?>
Probably the above code could be optimized further, comments are highly appreciated!
Thanks, Simon
----
Server IP: 194.153.94.10
Probable Submitter: 84.74.48.39
----
Manual Page -- http://www.php.net/manual/en/function.urlencode.php
Edit -- https://master.php.net/note/edit/82483
Del: integrated -- https://master.php.net/note/delete/82483/integrated
Del: useless -- https://master.php.net/note/delete/82483/useless
Del: bad code -- https://master.php.net/note/delete/82483/bad+code
Del: spam -- https://master.php.net/note/delete/82483/spam
Del: non-english -- https://master.php.net/note/delete/82483/non-english
Del: in docs -- https://master.php.net/note/delete/82483/in+docs
Del: other reasons-- https://master.php.net/note/delete/82483
Reject -- https://master.php.net/note/reject/82483
Search -- https://master.php.net/manage/user-notes.php