can you over use "str_replace" on one page?

Discussion in 'PHP' started by Deliwasista, Apr 30, 2006.

  1. #1
    I have an asp page that is being called from another site into my php page.

    The page arrives well no issues there. However there are a pile of links on the called page that I need to alter so they dont take the user back to the other persons site.

    I am currently changing every single url using "str_replace" to point back at pages on my site. We have pretty much the same event info so thats not going to be strange for the user.

    However this means theres going to be around 20 or so "str_replace" lines. Is that an abnormal use of "str_replace"? Is there a limit?

    Also there are several links I would like to remove altogether.. any ideas would be:) most appreciated!
     
    Deliwasista, Apr 30, 2006 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,827
    Likes Received:
    4,541
    Best Answers:
    123
    Trophy Points:
    665
    #2
    20 should be fine but I'd see if there was an ereg alternative.
     
    sarahk, Apr 30, 2006 IP
  3. Selkirk

    Selkirk Peon

    Messages:
    93
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can build two arrays to pass to str_replace to make multiple replacements at once. As long as you don't have too many replacements, this is pretty fast.

    Its best to stay away from ereg_replace, which is slower and has an uncertain future with unicode in PHP 6.

    preg_replace is a faster, more stable alternative for more complicated replacements. (A couple notes on preg_replace)

    str_replace is faster for simple cases. preg_replace is faster for complicated cases. You can find the cross over point by benchmarking each alternative. (if you care that much.)
     
    Selkirk, Apr 30, 2006 IP
  4. ideas_man

    ideas_man Active Member

    Messages:
    212
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    58
    #4
    I second Selkirk's views on ereg functions... str_replace is faster.

    Since you're essentially leeching (for want of a better word) a page from another site (which I hope you have permission for :p), if every page load is causing an external load from that third party site and then a lot of string manipulation is being done this could get quite cumbersome under any sort of reasonable traffic load.

    Unless the data you are taking is updated frequently in real-time, would it not be better to cache the page after you've downloaded and altered it. That way you would serve up a quick loading static page, plus you're not taking bandwidth from this third party site.You could perhaps set a cron job to perform the task at set intervals to ensure you always have up to date information.

    Just a thought :)
     
    ideas_man, Apr 30, 2006 IP
  5. pwaring

    pwaring Well-Known Member

    Messages:
    846
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    135
    #5
    There's no limit, but obviously every use of str_replace involves a function call and all the overheads that come with it. However, if you're pulling a page from another site then the bottleneck is more likely to be the connection between your site and theirs - I'd be more worried about that than a few calls to a PHP function.
     
    pwaring, Apr 30, 2006 IP
  6. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Just to expand on that, assuming you don't have permission to do so, it's not a bad idea to cache anyway as for every page view that you have, they will have one, too, coming from your server as the client...

    Depending on the amount of traffic that you get, it will be pretty easy for the source site to notice that something's not quite right...

    Alternatively, if you do have permission to do this, maybe the source would be willing to generate the page for you so that you don't have to do most of this work!
     
    TwistMyArm, Apr 30, 2006 IP
  7. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #7
    use preg_replace_all()

    it will find all the matches within the given expression and change them to the new one
     
    drewbe121212, Apr 30, 2006 IP
  8. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #8
    One second.

    Why are you including another site onto yours?
     
    drewbe121212, Apr 30, 2006 IP
  9. Deliwasista

    Deliwasista Member

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #9
    firstly thank you so much to you all for responding. I appreciate your time.

    Now in response to drew - I have a sporting events site that has teamed up with another site that displays multiple sporting sites events including an enter online function. We are using their results page, and events calendar page (with their permission) with the view to reducing the amount of info replication. I am also looking at "screenscraping" (is that the right term?) their secure enter online page.

    The calendar page has a list of events that change from time to time - dates change and events are added and removed throughout the year. The calendar page has a link on each event (about 20) plus other utility links that take the user away from my site to the other site. I dont want this - Eek dont want to make myself totally defunct! I do want to keep some links on the screenscraped page active however.

    I am interested in the suggestion of doing a "cron job" to control the refresh on the cache. Sounds like a handy thing to master!

    Im also interested in preg_replace_all() - however im a complete newbie -ive had a look at the link but still can not picture how to use it for a url. Could someone give me an example of how to apply this command to changing a url :p

    Selkirk thankyou for the benchmarking link - good site! - also your comment

    this is a portion example of what I currently have - im quite keen to learn more - how would I best go about building two arrays?

    $filestring = str_replace("../Images/", "http://www.theirsite.co.nz/Images/", $filestring);
    $filestring = str_replace("../events/eventdetail.asp?eventid=CKZ116075", "http://www.mysite.co.nz/events/volcano/index.php", $filestring);
    $filestring = str_replace("../events/eventdetail.asp?eventid=CKZ116991", "http://www.mysite.co.nz/events/lake/index.php", $filestring);
    $filestring = str_replace("h1", "p", $filestring);
    $filestring = str_replace("/h1", "/p", $filestring);
    echo "$filestring"; 
    PHP:
     
    Deliwasista, May 1, 2006 IP
  10. sarahk

    sarahk iTamer Staff

    Messages:
    28,827
    Likes Received:
    4,541
    Best Answers:
    123
    Trophy Points:
    665
    #10
    Now, my attention just got caught. Are you signed up for the PHPUG on Thursday...? I plan to make a rare appearance, if it's raining ;)

    http://php.meetup.com/10/
     
    sarahk, May 1, 2006 IP
  11. Deliwasista

    Deliwasista Member

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #11
    Hey Sarahk - sorry I missed you on phpug! And thanks for pointing out the site to me :) nice to know we have a great forum here at home! will be keeping an eye on it.

    :)
     
    Deliwasista, May 16, 2006 IP
  12. sarahk

    sarahk iTamer Staff

    Messages:
    28,827
    Likes Received:
    4,541
    Best Answers:
    123
    Trophy Points:
    665
    #12
    It didn't rain that night so my other committment kicked in.

    Will be there next month if I can. PHPUG is a "winter activity" ;)
     
    sarahk, May 16, 2006 IP