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

让人想犯罪 __ 提交于 2019-12-07 23:53:08

问题


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.


回答1:


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



回答2:


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>


来源:https://stackoverflow.com/questions/25616965/php-simple-html-dom-parser-how-to-get-tr-only-from-first-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!