Help with String (URI )Parsing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Help with String (URI )Parsing

Post by omniuni »

so, I have a calendar script that has next and previous buttons.

I have dealt with how to generate the appropriate links for next and previous, but I want to include other GET data that may be in the address. In other words:

Say I request the URI and after a bit of work, I get the following:

page=blah2&month=January&color=red

I want to remove the month variable as such:

page=blah2&color=red

which will then allow me to add the correct month for the "next month" link to the end.

the method should also remove year= and day=

So, anyone have an idea of a nice way to do this?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Help with String (URI )Parsing

Post by Christopher »

You can access this data from the superglobal array $_GET. Only take the values you want from the array.
(#10850)
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Help with String (URI )Parsing

Post by omniuni »

I had indeed considered that, but I do not know how many values are in the GET or what their names are.

I considered looping through the array, but I am not sure how to echo the key for each GET element.

I figured it would be easier to use some sort of regex to remove &[string1]=[string2] from the data where string1 = day, year, or month.

EDIT: This is now working. Thank you for the suggestion. It took some digging to get it right, but it does work now.
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Re: Help with String (URI )Parsing

Post by Phoenixheart »

$_GET is an associative array, just like normal ones.
I'll do like this:

Code: Select all

 
foreach ($_GET as $key => $value)
{
    if (!in_array($key, $known_keys))
    {
        unset($_GET[$key]);
    } 
}
 
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Help with String (URI )Parsing

Post by omniuni »

PHOENIXHEART, thank you.

I have it working now, but that's a good, efficient way to do it! I will definitely keep that in mind for future reference.

-OmniUni
Post Reply