<?php
include('simple_html_dom.php');
function curPageURL() {
$pageURL = 'http';
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
// Retrieve the DOM from a given URL
$html = file_get_html(curPageURL());
str_ireplace("http://martianguy.com","http://new.martianguy.com", $html);
?>
I am trying to replace all links with domain martianguy.com with new.martianguy.com (all href and scr attributes). Is it ok to use the current page url in file_get_html function? When I test this on my localhost, it does not do anything and times out after 30 seconds.
Seems to me this script would be recursive. If curPageUrl() returns the URL of the current page/script, and the script that calls curPageUrl() is on the same page, wouldn't the script be calling itself over http? If that's the case, it would explain the timeout after 30 seconds. The script calls itself over http recursively until you hit the php max_execution_time for the first call, which defaults 30 seconds.
Some suggestions:
If the script must be on this page, add a get variable to the URL in curPageUrl() then only run your replacement code if the variable isn't set:
if($_REQUEST['loaded'] != 1) {
$html = file_get_contents(curPageURL()."?loaded=1");
echo str_ireplace("oldURL","newURL", $html);
}
Use javascript, which runs on the page after the html has been loaded, and does the replacement on the client side.
This assumes that the content you're trying to replace is dynamic. If it's static, I'd save it to a file, and then use another script to make the replacements.
Hope that helps!
The str_ireplace function doesn't change strings in-place. You need to assign the output of that function to a variable.
file_get_html() returns a DOM object (http://simplehtmldom.sourceforge.net/manual_api.htm) while str_ireplace is expecting a string (http://www.php.net/manual/en/function.str-ireplace.php).
You have to loop through your DOM object and do the replace for each node. You can also just use file_get_contents (http://php.net/manual/en/function.file-get-contents.php) and replace every occurence of the url, but in this case it won't be only the src and href.