我能想到的唯一方法是删除所有 div 的孩子,然后打印左边的内容......方法如下:
// includes Simple HTML DOM Parser
include "simple_html_dom.php";
$text = '<div id="frame">
<span><b>Not needed</b></span>
Needed this content
<a href="#">Not needed</a>
</div>';
//Create a DOM object
$html = new simple_html_dom();
// Load HTML from a string
$html->load($text);
$content = $html->find('div#frame',0);
// Before
echo $content->innertext;
// Delete all unwanted children
foreach( $content->children() as $i => $unwantedTags ) {
echo "<br/>$i => ".$unwantedTags->tag;
$unwantedTags->outertext = '';
}
// After
echo "<br/>".$content->innertext;
// Clear dom object
$html->clear();
unset($html);
看到这个工作演示