Page 1 of 2
Replace text in html page at load [Solved]
Posted: Thu May 17, 2007 12:48 pm
by fullur
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
Posted: Thu May 17, 2007 12:55 pm
by andym01480
You are wanting to grab the contents of the html file into a string and then perform a str_replace on that variable and finally echo the variable.
So have a look at the php manual for
and then
and then post some code after trying!
Posted: Thu May 17, 2007 2:05 pm
by fullur
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):
Posted: Thu May 17, 2007 3:40 pm
by feyd
You are attempting to open a file outside of your accessible space it would appear. Maybe you got the path wrong?
Posted: Thu May 17, 2007 4:36 pm
by fullur
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:
Code: Select all
<?php
$resource = file_get_contents($_SERVER['PHP_SELF']);
print $resource;
?>
Posted: Thu May 17, 2007 8:21 pm
by feyd
PHP_SELF is a URL, not a local file path.
Posted: Fri May 18, 2007 2:38 am
by fullur
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?
Posted: Fri May 18, 2007 8:24 am
by feyd
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.
Posted: Fri May 18, 2007 4:53 pm
by fullur
Just the reverse actually, safe_mode is off and allow_url_fopen is on.
Posted: Thu May 24, 2007 2:31 pm
by fullur
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?
Posted: Thu May 24, 2007 4:10 pm
by fullur
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?
Posted: Thu May 24, 2007 5:59 pm
by andym01480
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!
Posted: Thu May 24, 2007 6:30 pm
by fullur
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.
Posted: Fri May 25, 2007 1:24 am
by andym01480
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.
Posted: Wed May 30, 2007 6:19 pm
by fullur
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="/newsite/css/colco.css" rel="stylesheet" type="text/css">
<script src="/newsite/scripts/email.js" language="javascript"></script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Columbia County, Oregon Home Page</title>
</head>
<body>
<div id="container">
<div id="header">
<?php require('/newsite/includes/logo.php'); ?>
<p>
Columbia County Courthouse<br>
230 Strand Street,
St. Helens, OR 97051<br>
(503) 397-7210 (888) 397-7210<br>
</p>
<?php require('/newsite/includes/nav.php'); ?>
</div>
<div id="content">
(pdf)
<?php
$resource = file_get_contents("/usr/local/4admin/apache/vhosts/httpdocs/newsite/pdf_icon_script_test.php");
$resource = str_replace("(pdf)", "PDF_LINK", $resource);
print $resource;
?>
</div>
<?php require('/newsite/includes/footer.php'); ?>
</div>
</body>
</html>
that is the code for my test script. I end up repeating the page twice and it looks very screwy.