Forum Moderators: phranque

Message Too Old, No Replies

Remove hyphens from a string.

...and pass as a parameter

         

mipapage

12:19 pm on Feb 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey all,

Given an URL, such as

example.com/see-spot-run-widgets-for-sale

Can I rewrite that to:

file.php?widget=see spot run&action=sell

The following is close, but it passes the hyphens:

RewriteRule ^([A-Za-z0-9-]+)-widgets-for-sale$ /file.php?widget=$1&action=sell[L]

I need to strip out the hyphens, but I'm not sure that it can be done. The number of hyphens will vary (from none to perhaps 5).

Any suggestions greatly appreciated...

jdMorgan

12:39 pm on Feb 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The threads turned up by this search [google.com] address a very similar problem, and may help to focus your question.

Jim

mipapage

1:45 pm on Feb 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Grr.. Thanks JD, I had searched, but not exclusively to WebmasterWorld... Sometimes I can be so silly...

mipapage

2:42 pm on Feb 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm.. That's kind of the opposite of what I'm after; I want it to take the hypenated correct URL and construct the spaced out url on the right side of the rule. The brute force [webmasterworld.com] method was something I was considering, but it seemed kinda, well, brute :)

The client will be linking up the correct, hyphenated URLs, what I need to do is transparently pass the spaced out values to the system, So I could brute forece out the hypens for spaces... (No 301's necessary in our case).

What I may do is see if they can simply learn to handle the hyphenated GET.

jdMorgan

3:48 pm on Feb 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If a back-end solution is possible in the script, then it would be more efficient to do it there. As you've seen from the code examples in the other threads, mod_rewrite has no efficient way to handle 'function looping' or recursion, both of which are easily implemented in scripting languages such as Perl or PHP.

As to "opposite" functions, that's easily solved by reversing/modifying the patterns and substitutions in the example rules (this is a do-it-yourself type of forum). :)

Jim

mipapage

4:01 pm on Feb 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As to "opposite" functions...

Oh I know, I wasn't sure I explained myself well as you said "may help to focus your question", which tells me that you thought my question was outtawhack :)

For me, it's pretty clear that they should handle this in the script; lets hope they feel the same way (they already have a nice collection of rewrites running, keeping the number lower would be better).