Search code examples
phphtml-tablehtml-parsingrows

Count the rows in a html table using php


I am looping through a html file using htmldomparsing looking for a table e.g

foreach($html->find('table') as $table)
{
  foreach($table->find('tr') as $tr)
  {
    $count = count($tr);
    $test = explode(" ",$count);
    echo $count;
  }
 }

I am trying to count the number of rows in the table, but everytime I use the count function it returns: 1111111 etc.

When counting the rows , is there any way I can count each row and the count would increment rather than throw out "1111...." etc. Any ideas?


Solution

  • This will work

    foreach($html->find('table') as $table){ 
         // returns all the <tr> tag inside $table
         $all_trs = $table->find('tr');
         $count = count($all_trs);
         echo $count;
    }