Search code examples
domsimple-html-dom

Skip div within div simple html dom


A page that I want to scrape data from has the following elements

<div class="content">
 <p>I want this</p>
 <p>I want this</p>
 <div class="row">
   <p>I do not want this</p>
   <p>I do not want this</p>

 </div>
</div>

I do not want paragraphs in the <div class="row"> This is what I have tried so far. Doesnt seem to work

foreach ($sample_html->find('div[class=entry-content]') as $content) {

                foreach($content->find('div[class=row]') as $row){

                    foreach ($row->find('p') as $skip) {

                        $skip->outertext = '';
                    }
                }

                foreach($content->find('p') as $paragraph){

                    //echo $paragraph;
                    $paragraphs .= '<p>'.$paragraph->innertext.'</p>';
                    echo $paragraphs;
                }


            }

Solution

  • Change

     $skip->outertext = '';
    

    to

     $skip->innertext= '';