2

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.

neattom
  • 364
  • 2
  • 7
  • find first `tr` throught php? or css? – mostafaznv Sep 02 '14 at 06:19
  • Could you share the code you've tried already? – scrowler Sep 02 '14 at 06:25
  • 2
    FYI, SimpleHTMLDom is a terrible library – Phil Sep 02 '14 at 06:30
  • @l'L'l looked at the code? It's also not libxml based and has some nasty memory issues. The standard DOM extension has everything you need and is well written and documented. Here's a good resource for XML / HTML parsers ~ http://stackoverflow.com/a/3577662/283366 – Phil Sep 02 '14 at 06:38
  • @Phil, actually, no I haven't — always used the standard one; good to know though. – l'L'l Sep 02 '14 at 06:41

2 Answers2

2

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
scrowler
  • 24,273
  • 9
  • 60
  • 92
0

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>
l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • Yes, this work fine for only one TR in first TABLE, but if i have more TR in first TABLE, it doesn't work :-( – neattom Sep 02 '14 at 06:40
  • Now I try it, but it doesn't work. Processed all TR in first and nested table :-( – neattom Sep 02 '14 at 07:48
  • So you're trying to only get the `` items in the first table without any nested tables, correct? — I think that's what you mean. Anyway, it should work that way now. – l'L'l Sep 02 '14 at 08:36