note 79154 added to function.parse-str
| From: | chris at mcfadyen dot ca | Date: | Wed, 14 Nov 2007 00:02:59 +0000 |
| Subject: | note 79154 added to function.parse-str | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-132239@lists.php.net to get a copy of this message | ||
I shouldn't've posted the original version, as it only worked with the most basic of query
strings.
This function will parse an html-safe query-like url string for variables and php-like ordered and
associative arrays. It places them into the global scope as parse_str does and adds minimal slashes
for database insertions without the triple-slash problems that magic quotes can produce (the reason
I had to write it in the first place). If you don't need the slashes, they're easy enough
to remove.
<?php
function parse_query($str) {
// Separate all name-value pairs
$pairs = explode('&', $str);
foreach($pairs as $pair) {
// Pull out the names and the values
list($name, $value) = explode('=', $pair, 2);
// Decode the variable name and look for arrays
list($name, $index) = split('[][]', urldecode($name));
// Arrays
if(isset($index)) {
// Declare or add to the global array defined by $name
global $$name;
if(!isset($$name)) $$name = array();
// Associative array
if($index != "") {
${$name}[$index] = addslashes(urldecode($value));
// Ordered array
} else {
array_push($$name, addslashes(urldecode($value)));
}
// Variables
} else {
// Declare or overwrite the global variable defined by $name
global $$name;
$$name = addslashes(urldecode($value));
}
}
}
?>
----
Server IP: 208.69.120.35
Probable Submitter: 216.27.187.242
----
Manual Page -- http://www.php.net/manual/en/function.parse-str.php
Edit -- https://master.php.net/note/edit/79154
Del: integrated -- https://master.php.net/note/delete/79154/integrated
Del: useless -- https://master.php.net/note/delete/79154/useless
Del: bad code -- https://master.php.net/note/delete/79154/bad+code
Del: spam -- https://master.php.net/note/delete/79154/spam
Del: non-english -- https://master.php.net/note/delete/79154/non-english
Del: in docs -- https://master.php.net/note/delete/79154/in+docs
Del: other reasons-- https://master.php.net/note/delete/79154
Reject -- https://master.php.net/note/reject/79154
Search -- https://master.php.net/manage/user-notes.php