note 94985 added to reserved.variables.request
| From: | JohnGalt at osu1 dot php dot net | Date: | Mon, 07 Dec 2009 01:36:34 +0000 |
| Subject: | note 94985 added to reserved.variables.request | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-163628@lists.php.net to get a copy of this message | ||
I wrote a function because I found it inconvenient if I needed to change a particular parameter
(get) while preserving the others. For example, I want to make a hyperlink on a web page with the
URL http://www.site.com/script.php?id=1&blah=blah+blah&page=1
and change the value of "page" to 2 without getting rid of the other parameters.
<?php
function add_or_change_parameter($parameter, $value)
{
$params = array();
$output = "?";
$firstRun = true;
foreach($_GET as $key=>$val)
{
if($key != $parameter)
{
if(!$firstRun)
{
$output .= "&";
}
else
{
$firstRun = false;
}
$output .= $key."=".urlencode($val);
}
}
if(!$firstRun)
$output .= "&";
$output .= $parameter."=".urlencode($value);
return htmlentities($output);
}
?>
Now, I can add a hyperlink to the page
(http://www.site.com/script.php?id=1&blah=blah+blah&page=1) like this:
<a href="<?php add_or_change_parameter("page", "2");
?>">Click to go to page 2</a>
The above code will output
<a href="?id=1&blah=blah+blah&page=2">Click to go to page
2</a>
Also, if I was setting "page" to a string rather than just "2", the value would
be urlencode()'d.
<a href="<?php add_or_change_parameter("page", "banana+split!");
?>">Click to go to page banana split!</a>
would become
<a href="?id=1&blah=blah+blah&page=banana+split%21">Click to go to
page banana split!</a>
----
Server IP: 69.147.83.197
Probable Submitter: 72.208.240.199
----
Manual Page -- http://www.php.net/manual/en/reserved.variables.request.php
Edit -- https://master.php.net/note/edit/94985
Del: integrated -- https://master.php.net/note/delete/94985/integrated
Del: useless -- https://master.php.net/note/delete/94985/useless
Del: bad code -- https://master.php.net/note/delete/94985/bad+code
Del: spam -- https://master.php.net/note/delete/94985/spam
Del: non-english -- https://master.php.net/note/delete/94985/non-english
Del: in docs -- https://master.php.net/note/delete/94985/in+docs
Del: other reasons-- https://master.php.net/note/delete/94985
Reject -- https://master.php.net/note/reject/94985
Search -- https://master.php.net/manage/user-notes.php