473,372 Members | 1,482 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,372 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 22917
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.