我正在使用PHP简单的HTML DOM类来解析html。
我想选择带有id="content"
内部文字的div,但是当我调用$selecrot->plaintext
时,它也会返回子div文本
示例HTML
<div id="content">
Hello World.
<div id="sub-content1">
Text I don't want to select.
</div>
<div id="sub-content2">
Text I don't want to select
</div>
</div>
示例代码
//suppose $html contains above html
$selector = $html->find("div#content", 0);
echo $selector->innertext;
//it outputs "Hello World. Text I don't want to select. Text I don't want to select"
//but
我只想要“Hello World”
答案 0 :(得分:3)
include_once('simple_html_dom.php');
$html = new simple_html_dom();
$text = '<div id="content">
Hello World.
<div id="sub-content1">
Text I don\'t want to select.
</div>
<div id="sub-content2">
Text I don\'t want to select
</div>
</div>';
$html->load($text);
$selector =$html->find("div#content",0)->find("*");
foreach($selector as $node){
$node->outertext = '';
}
$html->load($html->save());
$selector =$html->find("div#content",0);
echo $selector->innertext;