1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need to preg_replace "/|/"

Discussion in 'PHP' started by qwikad.com, Sep 29, 2015.

Thread Status:
Not open for further replies.
  1. #1
    preg_replace("/|/", "", $title);
    Code (markup):
    In case you're wondering what "/|/" is it's the sign on the same key as backslash, except you use the Shift key to use it.

    It doesn't do it if you just do it as it is in the example. There must be some way of removing it.
     
    Last edited: Sep 29, 2015
    qwikad.com, Sep 29, 2015 IP
  2. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #2
    Oops. As I posted this I realized that it also means "or" in regex. So escaping it "/\|/" should do it.
     
    qwikad.com, Sep 29, 2015 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    uhm... str_replace doesn't use regex.
    Why don't you just do
    
    str_replace('|','',$string);
    
    Code (markup):
    it works perfectly?
     
    PoPSiCLe, Sep 29, 2015 IP
    qwikad.com likes this.
  4. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #4
    Actually, if you look at my edits it will say: preg_replace, I later changed it to str_replace, but forgot to remove the word regex from the post. I am going to change it back to preg_replace to not confuse anyone any further.
     
    qwikad.com, Sep 29, 2015 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    Okay, that makes more sense - now my question becomes - why are you using preg_replace() for such a basic change? It's a waste of resources - unless you're not showing the whole code, and just parts of it. There's no point in using preg_replace for such simple removal-scenarios, when you have str_replace available.
     
    PoPSiCLe, Sep 29, 2015 IP
Thread Status:
Not open for further replies.