Page 1 of 1

Help with String (URI )Parsing

Posted: Wed Jul 30, 2008 12:13 am
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?

Re: Help with String (URI )Parsing

Posted: Wed Jul 30, 2008 1:00 am
by Christopher
You can access this data from the superglobal array $_GET. Only take the values you want from the array.

Re: Help with String (URI )Parsing

Posted: Wed Jul 30, 2008 1:20 am
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.

Re: Help with String (URI )Parsing

Posted: Wed Jul 30, 2008 2:35 am
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]);
    } 
}
 

Re: Help with String (URI )Parsing

Posted: Wed Jul 30, 2008 3:03 am
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