簡體   English   中英

去掉 <html> 和 <head> DOMDocument :: saveXML上的標簽

[英]Remove <html> and <head> tags at DOMDocument::saveXML

我有一部分結構不完整的html。 例:

<div id='notrequired'>
    <div>
        <h3>Some examples :-)</h3>
        STL is a library, not a framework.
    </div> 
    </p>
    </a>
    <a target='_blank' href='http://en.wikipedia.org/wiki/Library_%28computing%29'>Read more</a>;
</div>
<a target='_blank' href='http://en.wikipedia.org/wiki/Library_%28computing%29'>Read more</a>";

你可以在這里注意到我有意想不到的</p></a>標簽。

我嘗試了一段代碼來刪除<div id='notrequired'>並且它可以工作,但無法准確處理它。

這是代碼段:

function DOMRemove(DOMNode $from) {
            $from->parentNode->removeChild($from);
        }

        $dom = new DOMDocument();
        @$dom->loadHTML($text); //$text contains the above mentioned HTML

        $selection = $dom->getElementById('notrequired');
        if($selection == NULL){
            $text = $dom->saveXML();
        }else{
            $refine = DOMRemove($selection);
            $text = $dom->saveXML($refine);
        }

問題是$dom->saveXML保存為HTML內容:

       <?xml version="1.0" standalone="yes"?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
        <html>

<body>
            <a target="_blank" href="http://en.wikipedia.org/wiki/Library_%28computing%29">Read more</a>

    </body>    
    </html>

我只需要:

<a target='_blank' href='http://en.wikipedia.org/wiki/Library_%28computing%29'>Read more</a>

而不是<HTML><BODY>標簽。

我錯過了什么? 任何其他方式做得更好?

好的..我想我找到了解決方案。 方法可能不對,但是,它完成了工作!

正如Hakre指出的那樣,它與PHP的DomDocument中的innerHTML完全相同? ,這不是完全重復,但它給了我一個暗示使用這個想法。 謝謝你的建議。

它幫助我構建了以下解決方案:

function DOMRemove(DOMNode $from) {
    $from->parentNode->removeChild($from);
}

function DOMinnerHTML($element) 
{ echo "Ashwin";
    $innerHTML = ""; 
    $children = $element->childNodes; 
    foreach ($children as $child) 
    { 
        $tmp_dom = new DOMDocument(); 
        $tmp_dom->appendChild($tmp_dom->importNode($child, true)); 
        $innerHTML.=trim($tmp_dom->saveHTML()); 
    }
    return $innerHTML; 
}

$dom = new DOMDocument();
$dom->preserveWhiteSpace = false; 
@$dom->loadHTML($test);

$a = $dom->getElementById('step');

$b = DOMRemove($a);
$c = $dom->saveXML($b);

$domTable = $dom->getElementsByTagName("body"); 

foreach ($domTable as $tables) 
{ 
    $x = DOMinnerHTML($tables); 
    echo $x; 
}

如果輸入是:

<div id='step'>
    <div >
        <h3>Some examples :-(</h3>
        Blah blah blah...
    </div> </p>
    </a>
    <a target='_blank' href='#'>Read more</a>;
</div>
<div id='step2'>
    <div>
        <h3>Some examples :-) :-D</h3>
        Blah2 blah2 blah2...
    </div> </p> </a>
</div>
<a target='_blank' href='#'>Read more</a>
<a target='_blank' href='#'>Read more</a>
<a target='_blank' href='#'>Read more</a>

正如預期的那樣,輸出是:

<div id="step2">
    <div>
        <h3>Some examples :-) :-D</h3>
        Blah2 blah2 blah2...
    </div> 
</div>
<a target="_blank" href="#">Read more</a>
<a target="_blank" href="#">Read more</a>
<a target="_blank" href="#">Read more</a>

解決方案有效但可能不是最佳的。 有什么想法嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM