gpt4 book ai didi

PHP DomDocument 编辑所有链接

转载 作者:可可西里 更新时间:2023-10-31 22:12:50 28 4
gpt4 key购买 nike

我正在使用以下代码从另一个页面获取 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();

关于PHP DomDocument 编辑所有链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15469274/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com