To regex, or not to regex?

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
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

To regex, or not to regex?

Post by uberpolak »

Whole mess of crap to deal with, try to stay with me here...

The server I'm working with doesn't allow any htaccess changes, so to keep URLs somewhat user-friendly, I'm extracting the query string, and using what's in there as a variable, rather than full-out URL variable passing.

i.e. ?home instead of ?page=home

That much I have set up without a problem. The problem is when there is another variable in the query string (which happens if someone wants to switch the stylesheet). The query string ends up looking like this:

?home&s=1

It picks up s=1 okay, but doesn't know what to do with home, and the query string is taken as "home&s=1," which isn't a valid page, obviously. What I want to do is have it find "home" (I suppose it would consider this a variable without a value).

Having very little experience with regular expressions, I'm not sure if they have the capability I'm looking for (although, from what I understand, it makes sense that they would). What I'm asking is, how would I set up the expression to search for an unknown value?

Lot of explanation for one question, huh?
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

Post by uberpolak »

I achieved the desired result by parsing the query string, and going throught the variables with foreach(), but the code seems like it could be much more efficient. This isn't a super-high priority anymore, but if anyone has a solution I'd still be interested.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

You could have a query string like:

http://www.mysite.com/index.php?page=forum/posts/233/2

Explode $_GET['page'] to grab the args.

As well as making URLs more user friendly, parsing the query string makes it harder for tamperers to glean the function of each GET var.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

i hate "tamperers", they tend to make my job far harder then it should every be.......
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You could also grab everything before the first & (check the user comments):
http://php.net/manual/en/function.strstr.php

Mac
Post Reply