How to make str_replace to find multiple values

Discussion in 'PHP' started by jennypretty, Sep 24, 2012.

  1. #1
    Hello,

    I like to use str_replace to find multiple values at the same time but I could not get it worked.

    I know the syntax:
    str_replace(find,replace,string,count)

    On my City field, sometimes they enter "Las Vegas", "LA/Las Vegas". How do I replace a space ' ' and a splash '/' with an underscore '_'. Currently, I can make it work by one only. How do I find space ' ' or splash '/' in City field and replace with an underscore '_'.

    Here is my code:
    $values = str_replace("_"," ",$_GET[$profile_field['field_label']]);

    Thank you very much!
     
    jennypretty, Sep 24, 2012 IP
  2. YoEli

    YoEli Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Jenny,

    It's actually pretty easy to do thanks to str_replaces support for arrays.

    You can do it like this:

    $values = str_replace(array("_", " "), "_", $_GET[$profile_field['field_label']]);

    Details on the function can be found at the php.net site under the function--just search for any of them :)

    Hope this helps,

    Eli
     
    Last edited: Sep 24, 2012
    YoEli, Sep 24, 2012 IP
  3. jennypretty

    jennypretty Active Member

    Messages:
    584
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    58
    #3
    It is short, simple and It works perfect.
    Thanks very much!
     
    jennypretty, Sep 26, 2012 IP