str_replace & foreach or preg_replace

Discussion in 'PHP' started by Kyosys, Jan 25, 2009.

  1. #1
    Now, usually the obvious choice is str_replace, but I'm dealing with an array of maybe 40 - 50 elements here out of which only a couple will actually be in the text I'm replacing them in.

    What is more efficient? Looping through a bunch of times, even if many are unsuccessful or using regular expressions?

    thanks
     
    Kyosys, Jan 25, 2009 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    str_replace, with almost 100% certainty.

    Would have to be very odd for regular expressions to be faster.
     
    Danltn, Jan 25, 2009 IP
  3. harrisunderwork

    harrisunderwork Well-Known Member

    Messages:
    1,005
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    135
    #3
    While Loop over for loop, my two cents, will enable to keep some memory from wasting.
     
    harrisunderwork, Jan 25, 2009 IP
  4. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    alright, thanks for the input. I thought so. I guess I'll castrate the array before passing it to the loop as well.
     
    Kyosys, Jan 25, 2009 IP