note 109609 added to function.urldecode
| From: | bobbfwed at comcast dot net | Date: | Wed, 01 Aug 2012 23:31:02 +0000 |
| Subject: | note 109609 added to function.urldecode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-189566@lists.php.net to get a copy of this message | ||
Here is a function that converts a URL string into an indexed array like what is stored in the
$_GET, $_POST, etc. variables. The URL can be in any of these formats:
d_owner=true&d_ptype=%22true%22&ticket=five+44
or
&d_owner=true&d_ptype=%22true%22&ticket=five+44
or
?d_owner=true&d_ptype=%22true%22&ticket=five+44
or
file.php?d_owner=true&d_ptype=%22true%22&ticket=five+44
<?PHP
// decode URL string into array with values (like in $_GET or $_POST)
function urldecode_to_array ($url) {
$ret_ar = array();
if (($pos = strpos($url, '?')) !== false) // parse only what is after the ?
$url = substr($url, $pos + 1);
if (substr($url, 0, 1) == '&') // if leading with an amp, skip it
$url = substr($url, 1);
$elems_ar = explode('&', $url); // get all variables
for ($i = 0; $i < count($elems_ar); $i++) {
list($key, $val) = explode('=', $elems_ar[$i]); // split variable name from value
$ret_ar[urldecode($key)] = urldecode($val); // store to indexed array
}
return $ret_ar;
}
?>
I needed this for some AJAX work I was doing, so I thought it may be useful for others.
----
Server IP: 64.71.164.2
Probable Submitter: 99.107.218.6
----
Manual Page -- http://www.php.net/manual/en/function.urldecode.php
Edit -- https://master.php.net/note/edit/109609
Del: integrated -- https://master.php.net/note/delete/109609/integrated
Del: useless -- https://master.php.net/note/delete/109609/useless
Del: bad code -- https://master.php.net/note/delete/109609/bad+code
Del: spam -- https://master.php.net/note/delete/109609/spam
Del: non-english -- https://master.php.net/note/delete/109609/non-english
Del: in docs -- https://master.php.net/note/delete/109609/in+docs
Del: other reasons-- https://master.php.net/note/delete/109609
Reject -- https://master.php.net/note/reject/109609
Search -- https://master.php.net/manage/user-notes.php