Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Help with RegEx Expression

    Hi:

    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:
    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:
    Code:
    ...-([a-zA-Z]+)-...
    to include replacing all spaces with hyphens.

    Can someone help me with this, please.

    Thanks!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Code:
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Jul 2006
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Thank you but ..

    .. how do I add it to the code below:
    Code:
    ([a-zA-Z]+)
    ??

    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 .. ??
    Code:
    (/\s/g,"-"[a-zA-Z]+)
    Something like that?? (Seems wrong, however) ..

  • #4
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    878
    Thanks
    0
    Thanked 114 Times in 113 Posts
    Quote Originally Posted by spacepoet View Post
    .. how do I add it to the code below:

    Code:
    (/\s/g,"-"[a-zA-Z]+)
    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"

  • #5
    New Coder
    Join Date
    Jul 2006
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Ohhh ....

    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:

    12-Oz-Bottle
    Sun-Visor
    Hand-Made-Lanyard-Socks

    Possible?

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

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    Jul 2006
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Will try ...

    ... 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:
    Code:
    My-ITEM-Promo-Product.html
    the rewrite works fine and goes to the page ..

    With two or more words in the product:
    Code:
    My-ITEM-ANDOTHERWORD-Promo-Product.html
    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!



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •