note 86480 rejected from function.urlencode by danbrown
| From: | danbrown@php.net | Date: | Tue, 21 Oct 2008 21:34:50 +0000 |
| Subject: | note 86480 rejected from function.urlencode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-145548@lists.php.net to get a copy of this message | ||
Note Submitter: jasen at free dot net dot nz
----
I had need for a way to urlencode an array,
here's what I did.
the second (optional) parameter is for the variable name, it's mainly used for recursion over
arrays. you can ignore it and just use this instead of urlencode.
also included is the fix for # because sometimes I want to use # in a variable.
if you want to use the result it produces (normally you will) in HTML run it through
htmlspecialchars()
function urlcode($a,$wrap=''){
// like urlencode only better - it even does arrays!
if(!is_array($a))
{
if($wrap=='')return str_replace('#','%23',urlencode($a));
else return $wrap."=".str_replace('#','%23',urlencode($a));
}
$res='';
$amp='';
if($wrap=='')
foreach($a as $k => $v )
{
$res.=$amp.urlcode($v,urlcode($k));
$amp='&';
}
else
foreach($a as $k => $v )
{
$res.=$amp.urlcode($v,$wrap.'['.urlcode($k).']');
$amp='&';
}
return $res;
}
Does anyone know how to urlencode a resource ? :^)