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 9 of 9
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Exclamation preg replace help please.

    I have this code
    PHP Code:
    $short_title preg_replace("/.{2}(-inch|\"|"| inch | Inch).*/"""$alt_title) . "\n"
    which works a treat, but how do i get it to work if i want to remove everything after a . and 2 places before?

    i have tried this
    PHP Code:
    $short_title preg_replace("/.{2}(\.\").*/"""$alt_title) . "\n"
    which doesnt do anything i would like for example

    this string
    Samsung HT-TZ325R 5.1ch Home Cinema System - Black

    cut down to this
    Samsung HT-TZ325R

    any help appreciated
    Luke

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    Quote Originally Posted by LJackson View Post
    which works a treat, but how do i get it to work if i want to remove everything after a . and 2 places before?
    I think using a regular expression could be a little bit an overkill for this. At least if you would like to remove everything after a . and 2 places before. String functions are usually considered much faster ...
    PHP Code:
    <?php
    $str 
    'Samsung HT-TZ325R 5.1ch Home Cinema System - Black';

    $str substr($str,0,strpos($str,'.')-2);

    echo 
    $str// outputs "Samsung HT-TZ325R"
    ?>
    If I have not understood the problem correctly, could you explain please. I would be glad to provide any help of course.
    Last edited by SKDevelopment; 09-08-2009 at 06:09 PM.

  • Users who have thanked SKDevelopment for this post:

    LJackson (09-08-2009)

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    Still if you would really like to do it with a regular expression and preg_replace, I would give an example
    PHP Code:
    <?php
    $str 
    'Samsung HT-TZ325R 5.1ch Home Cinema System - Black';

    $str preg_replace('/^(.*?)(?=.\.).*$/','$1',$str);

    echo 
    $str;
    ?>
    I used a positive assertion (?=) here.

    Still I would recommend to use String functions as described in the previous post. Regular expressions for such a task could be an overkill.

  • Users who have thanked SKDevelopment for this post:

    LJackson (09-08-2009)

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    Hi SKDevelopment,

    thanks for your replys

    didnt relise this could be done with string functions, thank you

    Luke

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    one other question,

    how can i remove part of a string for example if i had this

    Joop Jump for Men 100ml Aftershave or this
    Joop Jump For Men 100ml Aftershave or this
    Paul Smith for Women 100ml EDP Perfume

    and remove just the (for Men, For Men, for Women, For Women) so i had this


    Joop Jump 100ml Aftershave or this
    Joop Jump 100ml Aftershave or this
    Paul Smith 100ml EDP Perfume

    thanks.

    p.s the strings will vary but most will have either for men or for women.

    thanks
    Luke

  • #6
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    If "for Men", "For Men", "for Women", "For Women" could be met in the string only once, you could use:
    PHP Code:
    <?php
    $str 
    'Joop Jump for Men 100ml Aftershave or this';

    $str str_replace(array('for Men ','For Men ','for Women ','For Women '),'',$str);

    echo 
    $str// outputs "Joop Jump 100ml Aftershave or this"
    ?>
    Please notice that this will replace all occurrences of "for Men", "For Men", "for Women", "For Women" in the string.

    Or you could use the case-insensitive version:
    PHP Code:
    <?php
    $str 
    'Joop Jump for Men 100ml Aftershave or this';

    $str str_ireplace(array('For Men ','For Women '),'',$str); 

    echo 
    $str// outputs "Joop Jump 100ml Aftershave or this"
    ?>
    Please notice that this would replace ll occurrences of "For Men", "For Women" in the case-insensitive way. So e.g. "FoR WoMeN" would be also replaced.

    If there are some more complicated strings and you would like to have more complicated replacement algorithm for them, please ask questions. I would be glad to provide any help.
    Last edited by SKDevelopment; 09-09-2009 at 09:45 AM.

  • #7
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    Hi mate, thanks for this,

    hopefully it wont get any more complicated than this lol, what im doing is grabbing aftershave/perfume products from an amazon feed and comparing them to other stores for my price comparision feature but for some reason they include for him and for her right in the middle of the string so that when searching other stores for that item im not finding any, but without that its finding results

    that should be it mate, thanks for your help...

    i may be back if i require any more help though

    cheers
    Luke
    Last edited by LJackson; 09-09-2009 at 02:55 PM.

  • #8
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    It is a pleasure. Yes, of course, please ask any questions. I would be glad if I could help.

  • #9
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,097
    Thanks
    297
    Thanked 12 Times in 12 Posts
    Will do thanks mate!!!


  •  

    Posting Permissions

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