php - simplehtmldom how can i extract between two elements -
i'm using simple html dom parser this. basically, i'm trying extract <p> tags between <table class="first"> , <div class="second">:
<div id="main> <table class="first"> <p> <p> <p> <div class="second"> <p> </div> in case, there 3 <p>s, might 2 or one. <p> can have id or class. can point me in right direction?
unfortunately cannot done directly simple-html-dom...
a workarround start @ starting node (ie table.first) , following siblings (or nodes of type x [it's specify if needed]) untill end node (ie div.second)
here's working code: (i modified input valid html code)
$input = <<<_data_ <div id="main"> <p>p1</p> <table class="first"> <tr> <td> <p>pintable</p> </td> </tr> </table> <p>p2</p> <p>p3</p> <p>p4</p> <div class="second">mydiv</div> <p>p5</p> </div> _data_; // create dom object $html = new simple_html_dom(); // load html string $html->load($input); // starting node $startpoint = $html->find('table.first', 0); // while current node has sibling while ( $next = $startpoint->next_sibling() ) { // , long it's different end node => div.second if ( $next->tag == 'div' && $next->class == 'second' ) break; else{ // print content echo $next->plaintext; echo '<br/>'; // , move next node $startpoint = $next; } } output
p2 p3 p4
Comments
Post a Comment