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!
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.)
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 ), 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
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.
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!
use preg_replace_all() it will find all the matches within the given expression and change them to the new one
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 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:
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/
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.
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"