PHP DomDocument编辑所有链接 [英] PHP DomDocument editing all links
本文介绍了PHP DomDocument编辑所有链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码从另一个页面抓取html并将其放入我的php页面:
I am using the following code to grab html from another page and place it into my php page:
$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\">
,这样我可以在其前面添加实际域。我该怎么做?
I want to change all instances of <a href="/somepath/file.htm">
so that I can prepend to it the actual domain instead. How can I do this?
因此,需要将它们更改为:< a href = http://mydomain.com /somepath/file.htm\">
代替。
So, it would need to change them to: <a href="http://mydomain.com/somepath/file.htm">
instead.
推荐答案
尝试类似的操作:
$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();
这篇关于PHP DomDocument编辑所有链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文