Page 1 of 1
file reference problems.
Posted: Fri Mar 13, 2009 3:28 pm
by debug
Hi-
I'm using this code to load in a website and then echo it out to hopefully have it looking exactly like the original site however because some of the images/css and other files contained with the HTML are referenced in a relative manner and not absolute it doesn't display the page properly.
Any ideas how I can add this information when loading in the file?
thanks
Code: Select all
<?php
$url = "http://www.reality-debug.co.uk/index.html";
if (!($fp = fopen($url, "r"))) { die("cannot open ".$url); }
while ($data = fread($fp, 500000)){
echo $data;
}
?>
Re: file reference problems.
Posted: Fri Mar 13, 2009 3:42 pm
by requinix
So, uh, why are you copying that site? I'm sure there's a good reason but there are many bad ones too.
Re: file reference problems.
Posted: Fri Mar 13, 2009 3:56 pm
by debug
Well that site contained within the above code is mine so I won't be 'copying' that site. Yeah it actually occurred to me just after posting that this kinda code could be pretty dodgy. All I'd like to do is write a really simple word substitution program kinda like the 'Dialectizer' (
http://rinkworks.com/dialect/).
Nothing sinister...

Re: file reference problems.
Posted: Fri Mar 13, 2009 4:24 pm
by requinix
Whatever script is doing this replacement, is it on reality-debug.co.uk too and just in a different path than /? Or are you just using it as a test?
Probably the simplest method is to find the </head> and just before it insert a
Code: Select all
<base href="http://www.reality-debug.co.uk/">
That will tell the browser that all links and such are relative to that path.
The harder method is to find all the links on the page and change them directly. Regular expressions would be great for that.
Re: file reference problems.
Posted: Fri Mar 13, 2009 4:47 pm
by debug
I was just using reality debug.co.uk as a test and it could be working on any site on the net.
How would I go around inserting <base href= $url > into the code?
It had occurred to me that using regular expressions might be required but base href seems like a much simpler way of doing it.
Thanks again!
Re: file reference problems.
Posted: Fri Mar 13, 2009 5:10 pm
by requinix
There are a billion ways of doing it:
str_replace,
substr and
strpos,
explode, and
regular expressions, just to name a few.
I'd go with str_replace.
Code: Select all
$new = str_replace("</head>", "<base href='http://www.reality-debug.co.uk/'></head>", $old);
Re: file reference problems.
Posted: Fri Mar 13, 2009 5:13 pm
by debug
thanks! that works perfectly.