PHP文档编辑所有链接

PHP文档编辑所有链接,php,domdocument,Php,Domdocument,我使用以下代码从另一个页面获取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'

我使用以下代码从另一个页面获取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);
我想更改
的所有实例,这样我就可以在它前面加上实际的域。我该怎么做

因此,它需要将它们改为:

尝试以下操作:

$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();

如果我是你,我将避免使用
DomDocument
,直接使用regex查找链接并编辑它。为什么?无论我在哪里遇到堆栈溢出,他们都说应该使用
DomDocument
来解决这个问题。你能给我一个如何使用正则表达式的例子吗?你可以为查找和替换任务创建额外的对象。额外的解析时间和内存消耗。尝试:但是
href
对于每个链接都是不同的,所以我只需要在它前面添加域。它会是:
$link->setAttribute('href',”http://mydomain.com/“+$link->getAttribute('href')?好的,很好,但我需要的是
$content
而不是整个文档。不管怎样,我是从你的回答中猜出来的。你明白了。谢谢:)