不完全に構造化された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>
タグではありません。
私は何が欠けていますか?それをより良くする他の方法はありますか?