I was working on this for a while, but someone beat me to the punch. My solution is not elegant, but if you wanted to get away from using whatever library that is you could write the same thing natively.
function count_tr($data, $p=0)
{
//$data is a string that we assume to have some html in it.
//$p is an indicator of where we are in the string
//$table_counts is an array of numRows
//$numRows is the number of rows we've counted this table
//$z is an indicator of where the next </table> tag is
$numRows = 0;
$table_counts = array();
$z = $p;
//First lets loop through it until we find a <table tag
while($p < strlen($data))
{
//we will break under the condition that we have found the end of the data
//If table is found, count its <tr tags. Else, break
if($p = strpos("<table", $data, $p) != FALSE)
{
//if we find a </table tag we want to know where it is.
//else we need to stop execution.
if($z = strpos("</table", $data, $p) != FALSE)
{
$numRows = 0;
while($p < strlen($data))
{
//find the position of the next <tr
if($p=strpos("<tr", $data, $p) != FALSE)
{
//if the <tr tag is within this <table tag then we count it
//else we break to the next iteration
if($p<$z)
{
$numRows++;
}
else
{
//This will take us to the next <table iteration.
break;
}
}
}
$table_counts[] = $numRows;
}
else
{
die("The data is not formatted correctly. That is beyond the scope of this snipped. Please write a validation script.");
}
}
else
{
die("There is not a <table tag.");
}
}
}