note 94985 modified in reserved.variables.request by danbrown
| From: | danbrown@php.net | Date: | Mon, 07 Dec 2009 16:13:51 +0000 |
| Subject: | note 94985 modified in reserved.variables.request by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-163668@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.example.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.example.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>
--was--
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>
http://php.net/manual/en/reserved.variables.request.php