我使用以下代码从另一个页面抓取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);
我想更改<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();