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.

str replace?

Discussion in 'PHP' started by Weirfire, Sep 5, 2005.

  1. #1
    If I was wanting to change all the occurances of <img='domain.com/category/category/ to <img='domain.com/category would I be wanting to use str_replace or is there a more efficient way of doing it? It would be quite a decent sized paragraph... possibly up to 1500 words.
     
    Weirfire, Sep 5, 2005 IP
  2. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I do a similar size task with str_replace and even loop through that size text to cover several replaces. ONly alternative I know is using reg exps but they are always loads more complicated so I never use them. Not sure about efficiency but I counted the time it took once and it's blindingly fast. We're talking milliseconds here.
     
    T0PS3O, Sep 5, 2005 IP
  3. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #3
    This has to be efficient though because the site we're talking about could be taking on 1,000's of users all at the same time wanting to open the paragraph.

    hmmm I might have a look at reg exps.

    I'll probably write a function for the replace and I can reg exps at a later date.
     
    Weirfire, Sep 5, 2005 IP
  4. bamb0lin

    bamb0lin Well-Known Member

    Messages:
    401
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #4
    I think there is no better alternative other than str_replace. Just asking !! Why do you have to replace the string everytime a visitors gets in? Cann't you just do it once ?? Why do you need to replace. Please give us more info maybe we can get around this problem!
     
    bamb0lin, Sep 5, 2005 IP
    Weirfire likes this.
  5. Stevo

    Stevo Peon

    Messages:
    9
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you are just doing simple text replacements then str_replace is the one to use. It will be faster (though probably not that noticeably) than using the likes of preg_replace. You should use preg_replace if you need to replace anything apart from a simple string.
     
    Stevo, Sep 5, 2005 IP
    Weirfire likes this.
  6. Dejavu

    Dejavu Peon

    Messages:
    916
    Likes Received:
    53
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Stevo is right, for simple text replacements str_replace wil be several times faster regular expressions. (regulare expressions must do some parsing first, str_replace not). It it is only about 1500 words or so, then you have nothing to worry about, it'l be very fast. (milliseconds)
     
    Dejavu, Sep 5, 2005 IP
    Weirfire likes this.
  7. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #7
    I concur. For a simple replacement on an already existing php string, str_replace is the way to go.

    If, however, you have your files uploaded and static on the server, it would probably be better to write a simple sed script to replace all the strings in your files. That way it's just a one time change and you aren't processing on every page access. http://forums.digitalpoint.com/showthread.php?t=24956
     
    nevetS, Sep 5, 2005 IP
    Weirfire likes this.
  8. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #8
    That is actually a very good point!

    I have managed to find a solution to the problem but thanks guys for your help. For some reason I always thought reg exps were faster than str replace. You learn something new every day.
     
    Weirfire, Sep 6, 2005 IP