Search code examples
phpsimple-html-dom

remove a child element in a element content with simple_html_dom


This is a sample html code:

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

   sentence 2

</div>

I just want to get sentence 2 (Not span tag and its content)

Is there any way to do this with simple_html_dom?

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

Solution

  • You can remove span tag using outertext:

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

    then you get the content

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

    edit:

    You can also try this way:

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

    $div will have your content.