php - Finding all consecutive occurences of a pattern using preg_match after a specific string -


i have huge html document has different tables unique table ids. like:

<table class="my_table" id="table_id1">   <tr class="odd"><td>line 1</td></tr>   <tr class="even"><td>line 2</td></tr>   <tr class="odd"><td>line 3</td></tr>   <tr class="even"><td>line 4</td></tr> </table> <table class="my_table" id="table_id2">   <tr class="odd"><td>line 1</td></tr>   <tr class="even"><td>line 2</td></tr>   <tr class="odd"><td>line 3</td></tr> </table> 

is possible using preg_match find html of rows of specific table?

i tried following code:

preg_match('/<table[^>]*id="table_id2">(<tr[^>]*><td>[^>]*<\/td><\/tr>)+/', $html, $matches);  //$html variable contains html. 

but returns output like:

array (     [0] => array         (             [0] => <table class="my_table" id="table_id2"><tr class="odd"><td>line 1</td></tr><tr class="even"><td>line 2</td></tr><tr class="odd"><td>line 3</td></tr>         )      [1] => array         (             [0] => <tr class="odd"><td>line 3</td></tr>         )  ) 

but need output this:

array (     [0] => array         (             [0] => <table class="my_table" id="table_id2"><tr class="odd"><td>line 1</td></tr><tr class="even"><td>line 2</td></tr><tr class="odd"><td>line 3</td></tr>         )      [1] => array         (             [0] => <tr class="odd"><td>line 1</td></tr>             [1] => <tr class="odd"><td>line 2</td></tr>             [2] => <tr class="odd"><td>line 3</td></tr>         )  ) 

is possible? please help.

you should not use regex parsing html. php has great tool - domdocument. using it, can many things, impossible/near impossible regex. sample like:

$shtml = '<table class="my_table" id="table_id1">   <tr class="odd"><td>line 1</td></tr>   <tr class="even"><td>line 2</td></tr>   <tr class="odd"><td>line 3</td></tr>   <tr class="even"><td>line 4</td></tr> </table> <table class="my_table" id="table_id2">   <tr class="odd"><td>line 1</td></tr>   <tr class="even"><td>line 2</td></tr>   <tr class="odd"><td>line 3</td></tr> </table>';  $rdoc   = new domdocument(); $rdoc->loadhtml($shtml); $sid    = 'table_id2'; //found table: $rtable = $rdoc->getelementbyid($sid); foreach($rtable->childnodes $ritem) {    //do item:    //var_dump($ritem); } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -