3

我有一部分 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>and<BODY>标签。

我错过了什么?还有其他更好的方法吗?

4

1 回答 1

2

好的..我想我想出了一个解决方案。方法可能不正确,但是,它确实有效!

正如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>

该解决方案有效,但可能不是最佳的。有什么想法吗?

于 2012-10-10T09:55:30.613 回答