View Full Version : Help with RegEx Expression

02-28-2012, 08:32 PM

I think this is where I need to ask this. I am doing a PHP/mod_rewrite and I need to tweak one of the RegEx's used in it but I don't know the syntax.

I have this bit of code:

<a href="http://www.mywebsite.com/Promotional.Products-Promotional.Items/Promotional-Products/<?php echo $product_id ?>-<?php echo str_replace(" ", "-", $myTitle) ?>-<?php echo $full_state ?>-Promotional-Products.html" title="<?php echo $myTitle?> <?php echo "$full_state"; ?>"><?php echo $myTitle?></a>

RewriteRule ^Promotional.Products-Promotional.Items/Promotional-Products/([0-9]+)-([a-zA-Z]+)-([a-zA-Z]+)-Promotional-Products.html$ Promotional.Products-Promotional.Items/Promotional-Products.php?product_id=$1&myTitle=$2&full_state=$3

I wanted to know how I can revise the middle RegEx:


to include replacing all spaces with hyphens.

Can someone help me with this, please.


Philip M
02-28-2012, 08:38 PM
var str = "abc def ghj xyz";
str = str.replace(/\s/g,"-");
alert (str);

But I don't see any spaces.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

02-28-2012, 09:12 PM
.. how do I add it to the code below:



It's hard to explain but it needs to be in there to match the A HREF line in order for the mod_rewrite to work properly ..

Can it be do this way .. ??


Something like that?? (Seems wrong, however) ..

02-28-2012, 10:31 PM
.. how do I add it to the code below:


Something like that?? (Seems wrong, however) ..

It IS wrong!
[a-zA-Z]+ is used to match one or more letters.

a-z is short for abcdefghijklmopqrstuvwxyz
A-Z is the same for uppercase letters
And + in this case means "one or more of the specified characters"

(/\s/g,"-"[a-zA-Z]+) would if it was possible mean:
replace any single whitespace-character, with a hyphen plus one or more letters
But what letter(s)???? and how many????

if it was possible to interprete, then "Hello World" could be changed to anything from "Hello-gWorld" to "Hello-hjGtbgYYTGNytbvIUYJMVgvNTCMTCYUTnmtgUIWorld"

02-28-2012, 10:44 PM
I definately do not want that ...

I'm trying to do where products coming out of the data base like this:

12 Oz Bottle
Sun Visor
Hand Made Lanyard Socks

Look like this:



Everything else works just as I need it to ....

Old Pedant
02-28-2012, 10:55 PM
Philip's answer in post #2 will do that.

But you seem to be wanting to do OTHER things beside just the space replacement.

And you also seem to be assuming that the syntax of regular expressions used in mod rewrite is the same as that used in JavaScript. I have no idea if that is true or not. You may be asking in entirely the wrong forum.

02-29-2012, 02:03 AM
... to see if I can work answer #2 into the .htaccess file .. not sure if it will work ..

I already sought an answer on PHP/Apache boards but to no avail ... :(

If my links have only 1 word in the product:


the rewrite works fine and goes to the page ..

With two or more words in the product:


It fails and goes to the 404 page.

I can only assume that the space-to-hyphen replace in the PHP code is fine (in the link itself; My-ITEM-ANDOTHERWORD-Promo-Product.html) is not matching the $myTitle in the .htaccess file (My-ITEM ANDOTHERWORD-Promo-Product.html) and that is where the disconnect is.

Everything else about the site is fine and works well. This isn't a deal-killer for the site.

It would just be nice to get this last piece in place, for even better SEO friendly URLs ...

It's been driving me nuts!


EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum