PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I am trying to make a script that will allow me to replace a specific string in an html page when it loads. specifically at the moment what I want to do is replace
(pdf)
with a link to a pdf icon when the page is loaded. I am pretty sure this is possible, but I don't really know where to start. I did several google and forum searches to try and get an idea, but I was probably searching for the wrong things. Anyway, any help would be greatly appreciated.
Phillip
Last edited by fullur on Thu May 31, 2007 11:26 pm, edited 1 time in total.
Thank you for your reply. I did try file_get_contents, I got this error:
Warning: file_get_contents() [function.file-get-contents]: open_basedir restriction in effect. File(/newsite/pdf_icon_script_test.php) is not within the allowed path(s):
I used $_SERVER['PHP_SELF'] and got that error. I have other PHP scripts that run from that same folder. None of them are very complex but that shouldn't really make a difference for this should it? Here is the exact script that gives that error:
Oh. Okay, so what should I be using? The URL method worked when I used one from an external site... Is there some reason why a URL will not work for the local site?
It's not possible to tell without knowing how the server is set up. At this point, I can only assume the server has safe_mode on and has allow_url_fopen off.
According to the PHP Manual file_get_contents is restricted by open_basedir. That's what the error was referencing. However, open_basedir is set to allow me to open from my /httpdocs folder. Shouldn't that allow me to open any file in my website?
Okay, so using a local file path I am able to open the page to read. I don't fully understand this as using a URL works for an external website. Would someone be able to explain that to me? Anyway, using this method I end up echoing the entire page. What I want to do is just replace a particular string in the html of the page with a link. Is that possible?
Once you have a variable containing file_get_contents data use str_replace to replace "(pdf)" with the link, then do whatever with it - which I assume is echo!
The problem with that method is that I am loading the page I want to replace the string on. e.g. I am loading example.php and replacing the string in example.php. Using the method we have been discussing, the page ends up being output twice.
We are a cunning lot here. I've learnt php by trying stuff, making mistakes, posting what I had done and then asking people to help. I could just post you some code... but I am not going to yet!
Could you post what you have please and then we will show you what's wrong.