Forum Moderators: phranque

Message Too Old, No Replies

Dashes and Regex

         

ukgimp

1:10 pm on Nov 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Fairly simple regex but what is the form for doing regex with potential for dashes in the variable.

For example:

rewriteRule ph\/([a-z-]+)\.php http:\/\/localhost\/ph\/detail\.php?var=$1 [L]

so that a var of "wordone" would be found as too would:

"wordone-wordtwo" or even a three dash set of words

Easy enough to mess with the var once you get it with ereg_replace but what about getting it.

Any chance of some pointers in these, something I will have to do more and more and I need to get a good understanding of it.

Cheers

ukgimp

4:00 pm on Nov 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Gets weirder

moving to an "_" things work some of the time. For example using the below regex works.

ph\/([a-z_]+)\/ http:\/\/localhost\/ph\/detail\.php?var=$1 [L]

But if I try to get the file to look like

ph/word1_word2.php is does not, it hangs whenb using:

ph\/([a-z_]+)\.php http:\/\/localhost\/ph\/detail\.php?var=$1 [L]

Where am I going wrong.

Cheers

brotherhood of LAN

4:06 pm on Nov 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hey Richard,

Not 100% sure what the desired outcome is, but I think the - should be escaped as it's used in the character classes to signify a range of characters.

I hear ya though, if only regex on PHP/apache/everything else was the same, could learn it once and be done with ;)

jdMorgan

10:51 pm on Nov 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like a plain-old regex problem to me. If you want to catch a word optionally followed by a dash and then another word, you have to get the 'optionally' boundary in the right place.

This might work a little better, if not perfectly:


RewriteCond (REQUEST_URI) !^/ph/detail
RewriteRule ^ph/(([a-z]+)(\-[a-z]+)*)\.php$ /ph/detail\.php?var=$1 [L]

Note the nested parentheses. In these cases, mod_rewrite counts left parentheses in order to assign back-reference variable numbers. So in this case, $1 is the whole wordone-wordtwo-wordthree, $2 is wordone, and $3-$9 would be each trailing -wordtwo, -wordthree, -wordfour, etc. individually if present. Up to eight -words are supported.

The RewriteCond prevents an infinite loop if any subrequests or external redirects are done.

Jim

ukgimp

12:26 pm on Nov 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks jd for your reply. That works up to a point. If I leave the file-name.php extension it does not wotk, but if I change it slightly in the regex to look for

file-name/

it works well.

Can I ask why the need for the first bit that you used.

My errot logs just shows a "file does not exist" message.

It feels close :)

ukgimp

1:44 pm on Nov 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Biggest spanner award goes to me.

No wonder the thing wont wotk, the folder is called

ph/

so a regex for \.php throws an error.

Still curious anout that first bit though JD.

jdMorgan

10:22 pm on Nov 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This first bit?

RewriteCond (REQUEST_URI)!^/ph/detail

It prevents the rewrite from happening if we are already in the /ph subdirectory running the 'detail' script. *If* you needed it, your rewrite would loop forever without it. Given the time-zone difference, I though I'd throw that in there just to be on the safe side.

I'm afraid I can't tell if the rest is working for you or not, UK and Texas idioms being different and all...

Jim

ukgimp

10:05 am on Nov 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



JD

It is working thanks. But that little snippet of info I think will come into use as I have to apply extra rules in that folder so when I get a long wait I will know what the form is.

Regards