简体   繁体   中英

simple html dom to exclude paragraph with class

I have this code and i want first paragraph as output I tried to filter with paragraph but I am getting second paragraph

I am only interested in first paragraph text.

   <div class="bq_fq_lrg" style="margin:0px">
     <p>this text i want.</p>
     <p class="bq_fq_a">
       <a href="xz.html">this text i dont want.</a>
     </p>
   </div>

I tried this but it is giving second paragraph

  foreach($html->find('div.bq_fq_lrg p[0]') as $e)

The $html variable is an instance of SimpleHtmlDom

I am getting the content of the paragraph like this:

 $op1 = $e->innertext . '<br>';

You can use the ! in attributes to get that particular value. Consider this example:

include 'simple_html_dom.php';
$html_string = '<div class="bq_fq_lrg" style="margin:0px">
     <p>this text i want.</p>
     <p class="bq_fq_a">
       <a href="xz.html">this text i dont want.</a>
     </p>
   </div>';
$html = str_get_html($html_string);
foreach($html->find('div.bq_fq_lrg p[!class]') as $value) {
    echo $value->innertext; // this text i want.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM