note 97190 added to function.urlencode

From: Date: Wed, 07 Apr 2010 15:46:14 +0000
Subject: note 97190 added to function.urlencode
Groups: php.notes 
Request: Send a blank email to php-notes+get-167835@lists.php.net to get a copy of this message
I wrote this simple function that creates a GET query (for URLS) from an array: <?php function encode_array($args) { if(!is_array($args)) return false; $c = 0; $out = ''; foreach($args as $name => $value) { if($c++ != 0) $out .= '&'; $out .= urlencode("$name").'='; if(is_array($value)) { $out .= urlencode(serialize($value)); }else{ $out .= urlencode("$value"); } } return $out . "\n"; } ?> If there are arrays within the $args array, they will be serialized before being urlencoded. Some examples: <?php echo encode_array(array('foo' => 'bar')); // foo=bar echo encode_array(array('foo&bar' => 'some=weird/value')); // foo%26bar=some%3Dweird%2Fvalue echo encode_array(array('foo' => 1, 'bar' => 'two')); // foo=1&bar=two echo encode_array(array('args' => array('key' => 'value'))); // args=a%3A1%3A%7Bs%3A3%3A%22key%22%3Bs%3A5%3A%22value%22%3B%7D ?> ---- Server IP: 85.19.212.230 Probable Submitter: 80.213.152.70 ---- Manual Page -- http://www.php.net/manual/en/function.urlencode.php Edit -- https://master.php.net/note/edit/97190 Del: integrated -- https://master.php.net/note/delete/97190/integrated Del: useless -- https://master.php.net/note/delete/97190/useless Del: bad code -- https://master.php.net/note/delete/97190/bad+code Del: spam -- https://master.php.net/note/delete/97190/spam Del: non-english -- https://master.php.net/note/delete/97190/non-english Del: in docs -- https://master.php.net/note/delete/97190/in+docs Del: other reasons-- https://master.php.net/note/delete/97190 Reject -- https://master.php.net/note/reject/97190 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#167835) next »