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><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 に答える