note 93877 deleted from reserved.variables.get by peehaa
| From: | peehaa@php.net | Date: | Sun, 28 Apr 2019 10:58:44 +0000 |
| Subject: | note 93877 deleted from reserved.variables.get by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210848@lists.php.net to get a copy of this message | ||
Note Submitter: Alberto Lepe dev at alepe dot com
----
This Function will help you to manage your GET parameters to facilitate coding and prevent
duplication. This is a basic version but it can be easily extended.
<?php
// Author: Alberto Lepe (www.alepe.com)
/* Process $_GET to preserve user custom parameters
* the arguments is a list of URL parameters that should be removed/changed from URL
* for example:
*
* URL = "index.php?s=1&fi=2&m=4&p=3
*
* if called: fixGet("s"); the result has to be: ?fi=2&m=4&p=3
* if called: fixGet("s&m"); the result has to be: ?fi=2&p=3
* if called: fixGet("s=4"); the result has to be: ?s=4&fi=2&m=4&p=3
* if called: fixGet("s=2&m"); the result has to be: ?s=2&fi=2&p=3
* if called: fixGet("s=&m=3"); the result has to be: ?s=&fi=2&m=3&p=3
* if called: fixGet("s=2&m="); the result has to be: ?s=2&fi=2&m=&p=3
* Special: when it ends with a =":" its to leave it open at the end
* (just first occurrence) to facilitate concatenation:
* if called: fixGet("s=2&m:"); the result has to be: ?s=2&fi=2&p=3&m
* if called: fixGet("s=2&m:="); the result has to be: ?s=2&fi=2&p=3&m=
*
* Usage with HTML (using the URL example above and $id = 99):
*
* <a href="index.php<?php echo fixGet('m=2&s&fi:=').$id ?>"
>Link</a>
* Explanation: change "m" to 2, delete "s" and "fi" gets the $id
value. ("p" is kept as it is not specified)
* will output: <a href='index.php?m=2&p=3&fi=99'>Link</a>
*/
public function fixGet($args) {
if(count($_GET) > 0) {
if(!empty($args)) {
$lastkey = "";
$pairs = explode("&",$args);
foreach($pairs as $pair) {
if(strpos($pair,":") !== false) {
list($key,$value) = explode(":",$pair);
unset($_GET[$key]);
$lastkey = "&$key$value";
} elseif(strpos($pair,"=") === false)
unset($_GET[$pair]);
else {
list($key, $value) = explode("=",$pair);
$_GET[$key] = $value;
}
}
}
return "?".((count($_GET) > 0)?http_build_query($_GET).$lastkey:"");
}
?>
To test, copy+paste the following code into testFixGet.php
<?php
/*
* Unit Test for fixGet()
*/
$cases = array (
0 => array("s" => 1, "fi" => 2, "m" => 4,
"p" => 3),
1 => array("s" => "", "fi" => "", "m"
=> 4, "p" => 3),
);
$test[0] = array(
"s" => "fi=2&m=4&p=3",
"s&m" => "fi=2&p=3",
"s=4" => "s=4&fi=2&m=4&p=3",
"s=2&m" => "s=2&fi=2&p=3",
"s=&m=3" => "s=&fi=2&m=3&p=3",
"s=2&m=" => "s=2&fi=2&m=&p=3",
"s=2&m:=" => "s=2&fi=2&p=3&m=",
"z=9" => "s=1&fi=2&m=4&p=3&z=9",
"z:" => "s=1&fi=2&m=4&p=3&z",
"s:&m=3" => "fi=2&m=3&p=3&s",
"s&m=3" => "fi=2&m=3&p=3",
);
$test[1] = array(
"s" => "fi=&m=4&p=3",
"s&m" => "fi=&p=3",
"s=4" => "s=4&fi=&m=4&p=3",
"s=2&m" => "s=2&fi=&p=3",
"s=&m=3" => "s=&fi=&m=3&p=3",
"s=2&m=" => "s=2&fi=&m=&p=3",
"s=2&m:=" => "s=2&fi=&p=3&m=",
"z=9" => "s=&fi=&m=4&p=3&z=9",
"z:" => "s=&fi=&m=4&p=3&z",
);
foreach($cases as $x => $value) {
echo "<hr> CASE: $x <hr>\n";
foreach($test[$x] as $arg => $expected) {
$_GET = $cases[$x];
$res = myForm::fixGet($arg);
echo (($res === "?".$expected)?"OK":"NG ($res)")."
[$arg]<br>\n";
}
}
?>