Skip to content
Advertisement

Add, change, and remove GET parameters from query string

Let’s say the current query string is the following

JavaScript

I need a function which can add and remove several parameters from the query string. For example:

JavaScript

should give

JavaScript

So, not only should the function be able to add new parameters (eyes=2&fingers=10), but it should also change the ones which are already present (car=black => car=purple).

All these new and changed parameters could be passed in the first argument of the function. Separated by “&”.

The second argument of the function should pass all the keys of the parameters which should be removed from the query string, if present.

I only managed the first part which can add and replace parameters. But maybe somebody has a more efficient way, including the second argument for removing things from the query string.

Actually in the end I need the complete current URL. That’s why I added PHP_SELF. Though the removing part is not there yet…

JavaScript

Advertisement

Answer

JavaScript

This should work for you, utilizing parse_str(), array_merge() and http_build_query()

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement