I have a string with a full URL including GET variables. Which is the best way to remove the GET variables? Is there a nice way to remove just one of them?
This is a code that works but is not very beautiful (I think):
$current_url = explode('?', $current_url);
echo $current_url[0];
The code above just removes all the GET variables. The URL is in my case generated from a CMS so I don't need any information about server variables.
How about a function to rewrite the query string by looping through the $_GET array
! Rough outline of a suitable function
Something like this would be good to keep handy for pagination links etc.
In my opinion, the best way would be this:
It checks if there is an 'i' GET parameter, and removes it if there is.
basename($_SERVER['REQUEST_URI'])
returns everything after and including the '?',In my code sometimes I need only sections, so separate it out so I can get the value of what I need on the fly. Not sure on the performance speed compared to other methods, but it's really useful for me.
How about:
Couldn't you use the server variables to do this?
Or would this work?:
Just a thought.
Another solution... I find this function more elegant, it will also remove the trailing '?' if the key to remove is the only one in the query string.
Tests:
Will output:
» Run these tests on 3v4l