PHP is a simple HTML element that selects only the inner text and not the child inne rtext

I am using PHP a simple HTML DOM class to parse html. I want to select a div with id="content"inner text, but when I call $selecrot->plaintext, it also returns the text sub div

HTML example

<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>

Code example

//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"

+4
source share
1 answer
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;
+3
source

Source: https://habr.com/ru/post/1545653/


All Articles