I have some PHP code which grabs a website's HTML code, then echos it to the screen. I'm looking for a way to scan the HTML, and then replace all href values with another value. For example, I have "http://somepage.com" which contains the HTML code <a href="http://somepage.com/somepage">Click me</a>
, however the value of the "href" part could change at any time. I want to echo the same HTML code, but replace the href
value with http://mywebsite.com/somepage
. How can I do that? I have this so far
$q = htmlentities($_GET['q']);
$html2 = "https://somewebsite.com/search/" . str_replace(' ', '%20', $q);
$html = file_get_contents($html2);
echo $html;
I've seen PHP DomDocument editing all links, however this returns an error for me
Warning: DOMDocument::loadHTMLFile(): I/O warning : failed to load external entity