Reputation: 23058
In my test.php I would like to provide a link back to the production script index.php, passing the same GET-parameters:
foreach ($_GET as $key => $val) {
$get .= "&$key=$val";
}
# this one is wrong - I want to replace just the first "&"
$get = str_replace("&", "?", $get);
echo '<p>You are viewing the test version.</p>
<p><a href="/index.php' . $get .
'">Return to production version</a></p>';
How do I replace just the 1st char in the $get
string?
Or maybe there is a nicer way to create a "self-link"?
Upvotes: 0
Views: 849
Reputation: 40944
You should look at $_SERVER['QUERY_STRING']
, which contains the query string from the request, except for the first ?
charater.
So, for instance, if you were to go to http://example.com/page.php?var1=val1&var2=val2, the contents of $_SERVER['QUERY_STRING']
would be var1=val1&var2=val2
Also, to replace the first character of the string, you can simply use:
if(strlen($string) > 0) // Unexpected behavior would occur with empty strings
{
$string[0] = '?'; // This would modify the first character of a string
}
Upvotes: 1
Reputation: 212
You could just do:
$get = 'index.php?' . $_SERVER['QUERY_STRING'];
Upvotes: 3
Reputation: 10583
$get = "";
foreach ($_GET as $key => $val) {
$get .= "&$key=$val";
}
if(strlen($get) > 0)
$get[0] = "?";
Upvotes: 0