neattom
neattom

Reputation: 364

PHP Simple HTML DOM Parser how to get TR only from first table

I have HTML code like following structure how in PHP to fetch TR only from first table using PHP Simple HTML DOM find method.

    <table width="100%" cellspacing="0" cellpadding="0" border="0" class="convertedTable">
      <tbody>
       <tr>
         <td>
           <table width="100%" cellspacing="0" cellpadding="0" border="0">
            <tbody>
              <tr>
               <td>...</td>
               </tr>
             </tbody>
            </table>
           </td>
          </tr>
<tr>
         <td>Some text</td>
          </tr>
<tr>...</tr>
....
         </tbody>
        </table>

I tried these conditions, but doesn't work.

`

$file->find('/body/table/tr')
$file->find('/body/table[!class]/tr')
$file->find('body table tr')

Thanks for your advice.

Upvotes: 2

Views: 4068

Answers (2)

l&#39;L&#39;l
l&#39;L&#39;l

Reputation: 47179

You should be able to do this easily with just a few lines:

$table = $html->find('table', 0);
foreach($table->find('table') as $tb) {
foreach($tb->find('tr') as $tr) {
        echo $tr;
    }
}

result:

<tr>
 <td>...</td>
</tr>

Upvotes: 0

scrowler
scrowler

Reputation: 24405

You could try something as simple as this:

foreach($html->find('tr') as $tr) {
    // do something with $tr
    break; // stop now
}

Or specify it like this:

$tr = $html->find('tr', 0); // zero based index selector

Upvotes: 2

Related Questions