I can't get PHP to search for and replace certian things within an extremely large variable.
Here's what I've got so far. It fetches the remote document perfectly, but whenever I simply can't modify it. Absolutely infuriating. I think it is something I'm doing wrong with eregi_replace, but I can't figure out what. Thanks in advance for any help you may be able to provide.
<?php
// Variables you need to modify:
// Location of this document (and a "?" @ the end)
$self_location = "http://localhost/simplescripts/proxy.php?";
// Open the URL contained in the query string for read only purposes.
$file = fopen ($QUERY_STRING, "r");
// Append http:// to the location of this document.
$self_loc = $self_location.'http://';
// If unable to open the file, display this message:
if (!$file) {
echo "<font size=+3>Unable to open remote file $QUERY_STRING. Please check the URL and try again.</font>\n";
exit;
}
// If the URL is valid...
while (!feof ($file)) {
// The number of characters is the number of characters is (approximately) the number of characters
// in Samuel Butler's translation of Homer's Illiad. You can make it as large as your heart desires
// since it is limited only by the ammout of memory your server allows.
$html = fgets ($file,793656);
// Get the directory of the remote file [so I can validate external references], then append
// it to the location of this document.
$directory = eregi_replace ("/(.*).", "/", $QUERY_STRING);
$dir = $self_location.$directory;
// Replace all occurances of http:// with the location of this page + http://. This makes all
// absolute links linked via this page.
$http_rep = eregi_replace ("http://", $self_loc, $html);
// Fetch external references through this page.
// SRC
$src = array ('src=',
'src="');
$src2 = array ('src='.$dir,
'src="'.$dir);
$src_rep = eregi_replace ($src, $src2, $http_rep);
// HREF
$href = array ('href=',
'href="');
$href2 = array ('href='.$dir,
'href="'.$dir);
$href_rep = eregi_replace ($href, $href, $http_rep);
// Store the most recent parse as $finished!!!
$finished = $href_rep;
// Display the parsed HTML!
echo $finished;
}
// Close the file that was opened earlier.
fclose($file);
?>
PS Do what you want with the code (if you think you can make it work...). I believe in open-source.