Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP simple HTML Dom select inner text only not child inne rtext

Tags:

html

dom

php

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"

like image 982
user2717054 Avatar asked Jun 23 '14 10:06

user2717054


1 Answers

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;
like image 53
Angel Bonev Avatar answered Oct 13 '22 14:10

Angel Bonev