file reference problems.

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!

Moderator: General Moderators

Post Reply
debug
Forum Newbie
Posts: 4
Joined: Fri Mar 13, 2009 1:25 pm

file reference problems.

Post 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;    
}
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: file reference problems.

Post 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.
debug
Forum Newbie
Posts: 4
Joined: Fri Mar 13, 2009 1:25 pm

Re: file reference problems.

Post 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... :)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: file reference problems.

Post 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.
debug
Forum Newbie
Posts: 4
Joined: Fri Mar 13, 2009 1:25 pm

Re: file reference problems.

Post 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!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: file reference problems.

Post 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);
debug
Forum Newbie
Posts: 4
Joined: Fri Mar 13, 2009 1:25 pm

Re: file reference problems.

Post by debug »

thanks! that works perfectly.
Post Reply