I'm using PHP simple HTML DOM class to parse html.
I want to select div with id="content"
inner text, but when I call $selecrot->plaintext
, it also return sub div text
Sample 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>
Sample code
//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
I want only "Hello World"
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With