2012-06-20 1 views
0

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(); 
?> 
+1

당신이 당신의 url_to_absolute() 함수를 게시하시기 바랍니다 수를 ? 그리고 스 니펫에서 $ url 변수가 존재하지 않습니다. – pp19dd

+0

내 url_to_absolute 기능 : http://nadeausoftware.com/articles/2008/05/php_tip_how_convert_relative_url_absolute_url#Code –

+0

my $ url 변수가 $ url = $ _GET [ 'url']으로 미리 설정되었습니다. –

답변

0

이 컬 옵션 + curl_init ($ URL을) 시도 :

<?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($url); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, TRUE); 
$contents = curl_exec($ch); 
curl_close(); 

$dom = new DOMDocument(); 
$dom->loadHTML($contents); 
//$dom->saveHTMLFile('dom_doc_test.html'); 


//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') . '<br />'; 
    echo "newhref = " . $newhref; //shows http://example.com/.... (good) 
} 

$anchors = $dom->getElementsByTagName('a'); 
foreach($anchors as $anchor) 
{ 
    echo "new2 = " . $anchor->getAttribute('href') . '<br />'; //returns http://example.com only 
} 

//print output 
echo @$dom->saveHTML(); 
?> 
0

그것은 당신의 url_to_absolute 기능에서 버그가 수 있어야합니다

여기 내 코드입니다.

function url_to_absolute($url, $href){ 
    return trim($url . $href); 
} 

$url = 'http://example.com'; 

$dom = new DOMDocument(); 
$dom->loadHTML('<html><body><a href="/path.html?q=hello&a=bye"></a><a href="/path2.html?before=34&after=44"></a></body></html>'); 

$anchors = $dom->getElementsByTagName('a'); 
foreach($anchors as $anchor){ 
    $href = $anchor->getAttribute('href'); 
    echo "href = " . $href . '<br />'; 
} 

echo '<br />'; 

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

    $newhref = $anchor->getAttribute('href'); 
    echo "newhref = " . $newhref . '<br />'; 
} 

echo '<br />'; 

$anchors = $dom->getElementsByTagName('a'); 
foreach($anchors as $anchor){ 
    echo "new2 = " . $anchor->getAttribute('href') . '<br />'; 
} 

그 결과는 다음과 같습니다 : 내 간단한 url_to_absolute이다

href = /path.html?q=hello&a=bye 
href = /path2.html?before=34&after=44 

newhref = http://example.com/path.html?q=hello&a=bye 
newhref = http://example.com/path2.html?before=34&after=44 

new2 = http://example.com/path.html?q=hello&a=bye 
new2 = http://example.com/path2.html?before=34&after=44 
+0

코드를 바꿨습니다. 간단한 url_to_absolute 함수로. 나는 여전히 같은 문제를 겪고있다. 내 스 니펫을 편집하여 내 코드가 지금 어떻게 보이는지 보여줍니다. –

관련 문제