Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Elegantly handling GET variables

how can I do it?

10:46 pm on Jan 15, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 30, 2008
votes: 0

I'm having trouble with a website that I have encountered on multiple occasions. I've checked the interwebs and WebmasterWorld with no luck.

My problem is how to parse a URL that contains multiple GET variables. I have not been able to come up with a universal solution that can update variables that are already in the url, or add them to the url if they don't exist. I've tried using preg_replace, and that seems to work with single replacements, but my systems are getting more complicated, and they don't work all the time.

Is there a PHP function or custom function that can help me out?

9:57 am on Jan 16, 2009 (gmt 0)

Full Member

10+ Year Member

joined:Feb 24, 2005
votes: 0

This is off the top of my head, so sorry if it could be done better.

If you want to alter a $_GET variable, alter it directly in the $_GET array. This keeps them all in once nice and tidy place.

Then to get your new full URL, use a function that takes the URL you want and adds to it all the $_GET variables, e.g.

function fullURL($url = "/")
foreach($_GET as $key => $value)
$url .= strpos($url, "?") === false ? "?" : "&";
$url .= $key . "=" . $value;
return url_encode($url);

So the use would be like,

<a href="<?php echo fullURL("/test/page.php"); ?>">My Link</a>

Like I said, someone probably knows a better way, I just came up with this!