PHP - str_replace - How to use a wildcard?

Discussion in 'Programming & Software Development' started by ChEEkY ChiNo, Aug 13, 2004.

  1. ChEEkY ChiNo

    ChEEkY ChiNo Member

    Joined:
    Sep 8, 2002
    Messages:
    2,143
    Location:
    Campbelltown, NSW
    How would you use a wildcard in a str_replace if this is possible?


    I am basically looking for some text, and replacing it. But the text may also contain som numbers on the end which i will need to replace as well....

    Something might look like the following:
    str_replace('stringtofind% ', 'REPLACE WITH THIS' 'Searching in this');

    Any ideas?
     
  2. yoink

    yoink Member

    Joined:
    Feb 19, 2002
    Messages:
    3,484
  3. OP
    OP
    ChEEkY ChiNo

    ChEEkY ChiNo Member

    Joined:
    Sep 8, 2002
    Messages:
    2,143
    Location:
    Campbelltown, NSW
    I dont understand anything on that article :(
     
  4. yoink

    yoink Member

    Joined:
    Feb 19, 2002
    Messages:
    3,484
  5. mcatalyst

    mcatalyst New Member

    Joined:
    Oct 18, 2011
    Messages:
    1
    As far as I now str_replace does not have wildcards.

    $string = 'This is old text.';
    $string = str_replace('old text', 'new text', $string);

    $string = 'This is old text.';
    $string = preg_replace('/old text/', 'new text', $string);
    (the slashes are telling the regex matching system to start and end)

    Both of those do the same thing. str_replace is always better to use for a straight replace because it uses less resources. When you get into wildcard situations you have no choice but to use regular expressions, so in your case I would do this...

    $string = 'This is old text123 stuff';
    $string = preg_replace('/old text.*? /', 'new text321 ', $string);

    This changes 'This is old text123 stuff' to 'This is new text321 stuff'.

    The .* is saying any type of character, the ? after it is saying stop at the next match after the ?. In this case the next match is a space but it could be a word or any other type of match. Without the ? it would replace everything after the text your looking for.

    regular expressions is well worth learning so in the previous post I would go through that regular expression tutorial when you have time.
     
  6. HyDrA

    HyDrA Member

    Joined:
    Jul 1, 2001
    Messages:
    3,821
    Location:
    SA
    Regular expressions might require a steeper learning curve but they are really worth it in the end. So powerful...
     
  7. ysu

    ysu Member

    Joined:
    Sep 27, 2005
    Messages:
    1,107
    Location:
    Central Coast
    I do agree, however, I've found that I need it fairly rarely only.
    Still, it's a handy thing, on one rare occasion it has saved me a lot of very tricky coding.
     
  8. danstheman

    danstheman Member

    Joined:
    Mar 11, 2002
    Messages:
    212
    Location:
    Sydney
  9. Foliage

    Foliage Member

    Joined:
    Jan 22, 2002
    Messages:
    32,093
    Location:
    Sleepwithyourdadelaide
    Google "how to use regular expressions in php" or "how to use regex in php".

    It might take you an hour to get your head around it, but it will be worth it.
     
  10. Herro

    Herro Member

    Joined:
    Nov 16, 2002
    Messages:
    748
    Location:
    Brisbane
  11. Herro

    Herro Member

    Joined:
    Nov 16, 2002
    Messages:
    748
    Location:
    Brisbane
  12. Foliage

    Foliage Member

    Joined:
    Jan 22, 2002
    Messages:
    32,093
    Location:
    Sleepwithyourdadelaide
    that is fucking amazing.

    :thumbup::thumbup::thumbup:

    edit: Sure did waste 10 minutes playing with it. Bookmarked.
     
    Last edited: Oct 20, 2011
  13. Herro

    Herro Member

    Joined:
    Nov 16, 2002
    Messages:
    748
    Location:
    Brisbane
    I have used this about 10 times in the last 2 days... thanks again.
     

Share This Page

Advertisement: