我有以下html:
<html> <body> bla bla bla bla <div id="myDiv"> more text <div id="anotherDiv"> And even more text </div> </div> bla bla bla </body> </html>
我要删除从开始<div id="anotherDiv">到结束为止的所有内容<div>。我怎么做?
<div id="anotherDiv">
<div>
使用本机DOM
$dom = new DOMDocument; $dom->loadHTML($htmlString); $xPath = new DOMXPath($dom); $nodes = $xPath->query('//*[@id="anotherDiv"]'); if($nodes->item(0)) { $nodes->item(0)->parentNode->removeChild($nodes->item(0)); } echo $dom->saveHTML();