473,416 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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:
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
4 22923
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: KinŽsole | last post by:
Hi I'm very new to VB (using VB6) I have two lists one blank and one containing names in the format of surname and then forename.I also have a combo box containing forenames.When I select a...
12
by: agent349 | last post by:
Hi, I'm fairly new to c++. I need user input in the form of dollar amounts, ie: "$10.00". I'd like to remove the dollar sign "$" then store the rest in a variable. How do I go about removing the...
1
by: Robin Tucker | last post by:
Hi, I have one stored procedure that calls another ( EXEC proc_abcd ). I would like to return a result set (a temporary table I have created in the procedure proc_abcd) to the calling procedure...
6
by: Ryu | last post by:
I created 2 objects from a custom collection class. IntCollection a = new IntCollection(); IntCollection b = new IntCollection(); a.FromInt32Array(someIntArray); b = a; However when ever I...
10
by: Segfahlt | last post by:
I have a fairly simple C# program that just needs to open up a fixed width file, convert each record to tab delimited and append a field to the end of it. The input files are between 300M and...
5
by: darrel | last post by:
(apologies if I've asked this before in here...I thought I had, but can't seem to find my original post). I'm trying to automate the process of picking some colors. I want to select one color,...
7
by: fcolon75 | last post by:
I'm an experienced Access user, but very new to coding VBA in Access. I'd like to do the following: 1) Develop a basic query in the query designer. 2) Call that query from a VBA script 3)...
7
by: pbd22 | last post by:
Hi. How Do I "UPDATE" a previously created string? I have a problem where an XML string created in an event handler fails because the string doesn't "UPDATE" each time the event hanlder fires,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.