PHP简单的HTML Dom选择内部文本而不是子内部文本

时间:2014-06-23 10:19:40

标签: php html dom

我正在使用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”

1 个答案:

答案 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;