tannerc Posted August 13, 2009 Share Posted August 13, 2009 How can I have a php file, which is included in another page, and reads the page it's pulled into, then replaces any string it finds in that page? Does that make sense? Is this possible with PHP? Is there a better way to go about reading/making the change? $file = __FILE__; $fh = fopen($file, 'r'); $content = fread($fh, filesize($file)); $content = str_replace('String to replace', 'Whatever replaces it', $content); Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 13, 2009 Share Posted August 13, 2009 What is the question? You posted code which appears fine. Where exactly are you having trouble? Quote Link to comment Share on other sites More sharing options...
tannerc Posted August 13, 2009 Author Share Posted August 13, 2009 The code I posted will be placed in a file, say "magic.php". The magic.php file will then be included in several pages through-out my website. I want the magic.php file to look at the file that it is being pulled into, search it for a string, replace the string, and then show the results. So the problem is: how can I have this script read whatever file it is in, then find a replace part of that file? I know it's possible with javascript, but I wanted to see if there was any way to do it with PHP. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 13, 2009 Share Posted August 13, 2009 I would recommend instead of including a file in another file, put the code to do this in a function, and pass it a parameter of what file to read. Rather than include, you'll call my_replace('file.php'); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.