我有一個涉及DOM的奇怪錯誤。我試圖迭代文檔中的每個href,並在必要時用絕對路徑替換它。問題是,在我使用$dom->setttribute()
後,getAttribute
返回更改後的值。然而,如果我saveHTML()
或使用getElementsByTagName和getAttribute再次查詢標籤,則值已從http://example.com/path.php?ccc截斷爲http://example.com。在PHP中使用DOM更改屬性無法正確保存
這裏是我的代碼:
<?php
//include 'url_to_absolute.php';
function url_to_absolute($url, $href) {
return trim($url . $href);
}
$url = 'http://example.com';
//$url = $_GET["url"];
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
@curl_close();
$dom = new DOMDocument();
$dom->loadHTML($contents);
//change the urls to absolute
$anchors = $dom->getElementsByTagName('a');
foreach($anchors as $anchor)
{
$href = $anchor->getAttribute('href');
$abs = url_to_absolute($url, $href);
$anchor->removeAttribute('href');
$anchor->setAttribute('href', $abs);
//changed
$newhref = $anchor->getAttribute('href');
echo "newhref = " . $newhref; //shows http://example.com/.... (good)
}
$anchors = $dom->getElementsByTagName('a');
foreach($anchors as $anchor)
{
echo "new2 = " . $anchor->getAttribute('href'); //returns http://example.com only
}
//print output
echo @$dom->saveHTML();
?>
難道您發佈url_to_absolute()函數,請?而且,在你的代碼片段中,$ url變量不存在。 – pp19dd
我的url_to_absolute函數我從這裏得到:http://nadeausoftware.com/articles/2008/05/php_tip_how_convert_relative_url_absolute_url#Code –
我的$ url變量被設置得更早$ url = $ _GET ['url'] –