By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,877 Members | 1,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,877 IT Pros & Developers. It's quick & easy.

Manipulating URL's GET Variables

P: n/a
Does anyone have a function for manipulating GET variables in a URL? I
want to be able to modify some parameters without affecting others.

An example of what I'm looking for:
Let's say the current url is:
www.mine.com/home.php?name=joe&page=7&theme=blue

I want to be able to run a function like:
$newurl = change_get($_SERVER["PHP_SELF"],"page","6");
and end up with:
www.mine.com/home.php?name=joe&page=6&theme=blue
and then run:
$newurl = change_get($_SERVER["PHP_SELF"],"theme","");
and end with:
www.mine.com/home.php?name=joe&page=6
and then even run:
$newurl = change_get($_SERVER["PHP_SELF"],"action","logout");
and end with:
http://www.mine.com/home.php?name=jo...&action=logout

The order of the GET variables isn't important, of course.

I thought I saw a function that does just this in the user-supplied
comments in the documentation but I did not bookmark it and have been
unable to find it.

While were about it, is there any way to *completely* avoid GET
variables while still being able to use clickable links?

--
- Michael J. Astrauskas

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi Michael!

On Wed, 24 Sep 2003 08:22:59 -0700, "Michael J. Astrauskas"
<tr****@cox.net> wrote:
Does anyone have a function for manipulating GET variables in a URL? I
want to be able to modify some parameters without affecting others.

An example of what I'm looking for:
Let's say the current url is:
www.mine.com/home.php?name=joe&page=7&theme=blue

I want to be able to run a function like:
$newurl = change_get($_SERVER["PHP_SELF"],"page","6");
and end up with:
www.mine.com/home.php?name=joe&page=6&theme=blue
and then run:
$newurl = change_get($_SERVER["PHP_SELF"],"theme","");
and end with:
www.mine.com/home.php?name=joe&page=6
and then even run:
$newurl = change_get($_SERVER["PHP_SELF"],"action","logout");
and end with:
http://www.mine.com/home.php?name=jo...&action=logout

The order of the GET variables isn't important, of course.

I thought I saw a function that does just this in the user-supplied
comments in the documentation but I did not bookmark it and have been
unable to find it.

While were about it, is there any way to *completely* avoid GET
variables while still being able to use clickable links?


Theres a class in the project in the signature which does that.

HTH,

Jochen
--
Jochen Daum - CANS Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Jul 17 '05 #2

P: n/a
"Michael J. Astrauskas" <tr****@cox.net> wrote in message
news:<3Ficb.8556$vj2.2073@fed1read06>...

Does anyone have a function for manipulating GET variables in a URL?
The first step in solving any problem is to formulate it sorrectly.
It seems to me that what you are trying to do is a simple string
replacement problem...
I want to be able to modify some parameters without affecting others.

An example of what I'm looking for:
Let's say the current url is:
www.mine.com/home.php?name=joe&page=7&theme=blue

I want to be able to run a function like:
$newurl = change_get($_SERVER["PHP_SELF"],"page","6");
and end up with:
www.mine.com/home.php?name=joe&page=6&theme=blue
and then run:
$newurl = change_get($_SERVER["PHP_SELF"],"theme","");
and end with:
www.mine.com/home.php?name=joe&page=6
and then even run:
$newurl = change_get($_SERVER["PHP_SELF"],"action","logout");
and end with:
http://www.mine.com/home.php?name=jo...&action=logout


Use regular expressions; personally, I am not that good with them,
so here is an alternative:

function change_get ($url, $newname, $newvalue) {
list ($path, $query) = explode ('?', $url);
$pairs = explode ('&', $query);
$n = count ($pairs);
$replaced = FALSE;
for ($i = 0; $i < $n; $i++) {
list ($name, value) = explode ('=', $pairs[$i]);
if ($name == $newname) {
$pairs[$i] = $newname . '=' . $newvalue;
$replaced = TRUE;
break;
}
}
if (!$replaced) {
$pairs[] = $newname . '=' . $newvalue;
}
$newget = $path . '?' . implode ('&', $pairs);
return $newget;
}

One reason I would prefer this alternative to regular expressions
is that if the requested variable is not found in the URL, this
function appends the "$newname=$newvalue" pair to the URL.

Cheers,
NC
Jul 17 '05 #3

P: n/a
On Wed, 24 Sep 2003 08:22:59 -0700, "Michael J. Astrauskas" <tr****@cox.net>
wrote:
Does anyone have a function for manipulating GET variables in a URL? I
want to be able to modify some parameters without affecting others.

An example of what I'm looking for:
Let's say the current url is:
www.mine.com/home.php?name=joe&page=7&theme=blue

I want to be able to run a function like:
$newurl = change_get($_SERVER["PHP_SELF"],"page","6");
and end up with:
www.mine.com/home.php?name=joe&page=6&theme=blue
and then run:
$newurl = change_get($_SERVER["PHP_SELF"],"theme","");
and end with:
www.mine.com/home.php?name=joe&page=6
and then even run:
$newurl = change_get($_SERVER["PHP_SELF"],"action","logout");
and end with:
http://www.mine.com/home.php?name=jo...&action=logout

The order of the GET variables isn't important, of course.

I thought I saw a function that does just this in the user-supplied
comments in the documentation but I did not bookmark it and have been
unable to find it.
Had to write something similar in ASP recently (yuk) - rewriting it in PHP,
it's definitely simpler:

<pre>
<?php

function getArrayToUrl(&$get, $base, $separator = '&amp;') {
foreach ($get as $key => $value)
if (is_array($value))
foreach ($value as $subvalue)
$vars[] = $key . '[]=' . urlencode($subvalue);
else
if ($value)
$vars[] = $key . '=' . urlencode($value);

return $base . '?' . implode($separator, $vars);
}

function changeGet($base, $key, $value) {
$_GET[$key] = $value;
return getArrayToUrl($_GET, $base);
}

var_dump($_GET);
var_dump(getArrayToUrl($_GET, $_SERVER['PHP_SELF']));

var_dump(changeGet($_SERVER["PHP_SELF"],"page","6"));
var_dump(changeGet($_SERVER["PHP_SELF"],"theme",""));
var_dump(changeGet($_SERVER["PHP_SELF"],"action","logout"));

?>
</pre>

Output:

array(3) {
["name"]=>
string(3) "joe"
["page"]=>
string(1) "7"
["theme"]=>
string(4) "blue"
}
string(51) "/~andyh/test.php?name=joe&page=7&theme=blue"
string(51) "/~andyh/test.php?name=joe&page=6&theme=blue"
string(36) "/~andyh/test.php?name=joe&page=6"
string(54) "/~andyh/test.php?name=joe&page=6&action=logout"
While were about it, is there any way to *completely* avoid GET
variables while still being able to use clickable links?


Can't think of a way without nasty javascript+cookie manipulation in onClick
events.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #4

P: n/a
On Wed, 24 Sep 2003 23:35:55 +0100, Andy Hassall <an**@andyh.co.uk> wrote:
On Wed, 24 Sep 2003 08:22:59 -0700, "Michael J. Astrauskas" <tr****@cox.net>
wrote:
Does anyone have a function for manipulating GET variables in a URL? I
want to be able to modify some parameters without affecting others.
else
if ($value)
$vars[] = $key . '=' . urlencode($value);


On second thoughts I don't like this conditional; I'd drop it out:

else
$vars[] = $key . '=' . urlencode($value);

If you want to remove variables that ought to go in the changeGet function,
removing it from the $get array.

(And it doesn't work correctly for numeric values when they're zero anyway)

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.