gadss
gadss

Reputation: 22499

How to remove selected GET value from a URL

I have this line of code:

echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']);

and this will return:

http://example.com?pageview=myview&edit=true&message=tester

my question is that how can I remove the message=tester from the echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?

any idea please.. any help will be appreciated.

Upvotes: 0

Views: 96

Answers (2)

Yoandro Gonzalez
Yoandro Gonzalez

Reputation: 372

This is the general solution to remove any GET parameter, explode the whole URL by ? then get the parameters and lately locate the key you want to remove

$url = explode("?",str_replace( '%7E', '~', $_SERVER['REQUEST_URI']));
$params = explode("&",$url[1]);
foreach ($params as $key=>$value) {
   if (strstr($value,"message=")===0) unset($params[$key]);
}
$url[1]=implode("&",$params);
echo implode("?",$url);

That's it. In case you want to delete the parameter if it has an specific value do something like:

if (strstr($value,"message=tester")===0) unset($params[$key]);

Upvotes: 0

kittykittybangbang
kittykittybangbang

Reputation: 2400

If all you need to do is remove a known substring from the end of a string, you can use rtrim().

From the PHP docs:

rtrim — Strip whitespace (or other characters) from the end of a string

rtrim() accepts 2 parameters, the second of which is optional. The first is the string, and the second, if present, is the substring to be removed from the end of the string.

So, in your case:

echo rtrim(str_replace( '%7E', '~', $_SERVER['REQUEST_URI']),'message=tester');

This will return:

http://example.com?pageview=myview&edit=true&

Upvotes: 1

Related Questions