How to remove GET-variables with PHP

0 votes

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.

Oct 27, 2020 in PHP by kartik
• 37,520 points
2,829 views

1 answer to this question.

0 votes

Hello @kartik,

Try this:

preg_replace('/\\?.*/', '', $str)

Hope it works!!

answered Oct 27, 2020 by Niroj
• 82,800 points