Re: Manipulating URL's GET Variables
On Wed, 24 Sep 2003 23:35:55 +0100, Andy Hassall <andy@andyh.co. uk> wrote:
[color=blue]
>On Wed, 24 Sep 2003 08:22:59 -0700, "Michael J. Astrauskas" <trevie@cox.net >
>wrote:
>[color=green]
>>Does anyone have a function for manipulating GET variables in a URL? I
>>want to be able to modify some parameters without affecting others.[/color][/color]
[color=blue]
> else
> if ($value)
> $vars[] = $key . '=' . urlencode($valu e);[/color]
On second thoughts I don't like this conditional; I'd drop it out:
else
$vars[] = $key . '=' . urlencode($valu e);
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 (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Manipulating URL's GET Variables
Collapse
This topic is closed.
X
X
-
Guest replied -
Guest repliedRe: Manipulating URL's GET Variables
On Wed, 24 Sep 2003 08:22:59 -0700, "Michael J. Astrauskas" <trevie@cox.net >
wrote:
[color=blue]
>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:
> https://www.mine.com/home.php?name=j...e=7&theme=blue
>
>I want to be able to run a function like:
> $newurl = change_get($_SE RVER["PHP_SELF"],"page","6") ;
>and end up with:
> https://www.mine.com/home.php?name=j...e=6&theme=blue
>and then run:
> $newurl = change_get($_SE RVER["PHP_SELF"],"theme","") ;
>and end with:
> www.mine.com/home.php?name=joe&page=6
>and then even run:
> $newurl = change_get($_SE RVER["PHP_SELF"],"action","logo ut");
>and end with:
> https://www.mine.com/home.php?name=j...&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.[/color]
Had to write something similar in ASP recently (yuk) - rewriting it in PHP,
it's definitely simpler:
<pre>
<?php
function getArrayToUrl(& $get, $base, $separator = '&') {
foreach ($get as $key => $value)
if (is_array($valu e))
foreach ($value as $subvalue)
$vars[] = $key . '[]=' . urlencode($subv alue);
else
if ($value)
$vars[] = $key . '=' . urlencode($valu e);
return $base . '?' . implode($separa tor, $vars);
}
function changeGet($base , $key, $value) {
$_GET[$key] = $value;
return getArrayToUrl($ _GET, $base);
}
var_dump($_GET) ;
var_dump(getArr ayToUrl($_GET, $_SERVER['PHP_SELF']));
var_dump(change Get($_SERVER["PHP_SELF"],"page","6") );
var_dump(change Get($_SERVER["PHP_SELF"],"theme","") );
var_dump(change Get($_SERVER["PHP_SELF"],"action","logo ut"));
?>
</pre>
Output:
array(3) {
["name"]=>
string(3) "joe"
["page"]=>
string(1) "7"
["theme"]=>
string(4) "blue"
}
string(51) "/~andyh/test.php?name=j oe&page=7&theme =blue"
string(51) "/~andyh/test.php?name=j oe&page=6&theme =blue"
string(36) "/~andyh/test.php?name=j oe&page=6"
string(54) "/~andyh/test.php?name=j oe&page=6&actio n=logout"
[color=blue]
>While were about it, is there any way to *completely* avoid GET
>variables while still being able to use clickable links?[/color]
Can't think of a way without nasty javascript+cook ie manipulation in onClick
events.
--
Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Leave a comment:
-
Guest repliedRe: Manipulating URL's GET Variables
"Michael J. Astrauskas" <trevie@cox.net > wrote in message
news:<3Ficb.855 6$vj2.2073@fed1 read06>...[color=blue]
>
> Does anyone have a function for manipulating GET variables in a URL?[/color]
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...
[color=blue]
> 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:
> https://www.mine.com/home.php?name=j...e=7&theme=blue
>
> I want to be able to run a function like:
> $newurl = change_get($_SE RVER["PHP_SELF"],"page","6") ;
> and end up with:
> https://www.mine.com/home.php?name=j...e=6&theme=blue
> and then run:
> $newurl = change_get($_SE RVER["PHP_SELF"],"theme","") ;
> and end with:
> www.mine.com/home.php?name=joe&page=6
> and then even run:
> $newurl = change_get($_SE RVER["PHP_SELF"],"action","logo ut");
> and end with:
> https://www.mine.com/home.php?name=j...&action=logout[/color]
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=$newv alue" pair to the URL.
Cheers,
NC
Leave a comment:
-
Guest repliedRe: Manipulating URL's GET Variables
Hi Michael!
On Wed, 24 Sep 2003 08:22:59 -0700, "Michael J. Astrauskas"
<trevie@cox.net > wrote:
[color=blue]
>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:
> https://www.mine.com/home.php?name=j...e=7&theme=blue
>
>I want to be able to run a function like:
> $newurl = change_get($_SE RVER["PHP_SELF"],"page","6") ;
>and end up with:
> https://www.mine.com/home.php?name=j...e=6&theme=blue
>and then run:
> $newurl = change_get($_SE RVER["PHP_SELF"],"theme","") ;
>and end with:
> www.mine.com/home.php?name=joe&page=6
>and then even run:
> $newurl = change_get($_SE RVER["PHP_SELF"],"action","logo ut");
>and end with:
> https://www.mine.com/home.php?name=j...&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?[/color]
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/
Leave a comment:
-
Manipulating URL's GET Variables
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:
I want to be able to run a function like:
$newurl = change_get($_SE RVER["PHP_SELF"],"page","6") ;
and end up with:
and then run:
$newurl = change_get($_SE RVER["PHP_SELF"],"theme","") ;
and end with:
and then even run:
$newurl = change_get($_SE RVER["PHP_SELF"],"action","logo ut");
and end with:
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
Tags: None
Leave a comment: