我使用以下代码从另一个页面抓取html并将其放入我的php页面:
$doc = new DomDocument;
// We need to validate our document before refering to the id
$doc->validateOnParse = true;
$doc->loadHtml(file_get_contents('{URL IS HERE}'));
$content = $doc->getElementById('form2');
echo $doc->SaveHTML($content);
Run Code Online (Sandbox Code Playgroud)
我想更改所有实例,<a href="/somepath/file.htm">以便我可以在其前面添加实际域.我怎样才能做到这一点?
因此,它需要将它们更改为: <a href="http://mydomain.com/somepath/file.htm">相反.
尝试类似的东西:
$xml = new DOMDocument();
$xml->loadHTMLFile($url);
foreach($xml->getElementsByTagName('a') as $link) {
$oldLink = $link->getAttribute("href");
$link->setAttribute('href', "http://mydomain.com/" . $oldLink);
}
echo $xml->saveHtml();
Run Code Online (Sandbox Code Playgroud)