Php 使用simple_html_dom删除元素内容中的子元素

Php 使用simple_html_dom删除元素内容中的子元素,php,simple-html-dom,Php,Simple Html Dom,这是一个示例html代码: <div class="leadContent"> <span> sentence 1 </span> sentence 2 </div> 您可以使用outertext删除span标记: $html->find('div.leadContent span', 0)->outertext = ''; 然后你得到了内容 $html->find('div.leadContent', 0)-

这是一个示例html代码:

<div class="leadContent">
   <span> sentence 1 </span>

   sentence 2

</div>

您可以使用outertext删除span标记:

$html->find('div.leadContent span', 0)->outertext = '';
然后你得到了内容

$html->find('div.leadContent', 0)->innertext;
编辑:

您也可以这样尝试:

$div = $html->find('div.leadContent', 0);
$div->find('span',0)->outertext = '';

$div将包含您的内容。

您也可以尝试这种方式

echo $html->find('text',2)->plaintext;

编辑了我的答案,添加了另一个选项,您可以尝试。我已经有一段时间没有使用simple_html_dom了,但这是删除不需要的元素的方法,类似这样的方法应该可以工作。
echo $html->find('text',2)->plaintext;