Two (similar) ways you can do this:
1) By counting the <tr>
s and combine each 10 of them, disregard its <td>
number:
$doc=new DOMDocument();
$doc->loadHTML($html);
$xpath=new DOMXPath($doc);
echo "<table>\n";
/* 10 is the row count */
for($i=0;$i<10;$i++)
{
echo "<tr>\n";
/* 10 is the column count */
foreach($xpath->query('//table[@id="myTable"]/tr[position()>'.($i*10).' and position()<'.(($i+1)*10+1).']') as $tr)
{
echo "\t<td>";// "\t" to make it look nice
$tds=array();
foreach($tr->childNodes as $td)
{
if($td->nodeName!="td") continue;
$tds[]=$td->firstChild->nodeValue;
}
echo implode("<br />",$tds);
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>";
2) By counting the <td>
s and combine each 3 of them into a new <td>
, combine each 30 of them into a new <tr>
, disregard the <tr>
s:
$doc=new DOMDocument();
$doc->loadHTML($html);
$xpath=new DOMXPath($doc);
echo "<table>\n";
$i=0;
$tds=array();
foreach($xpath->query('//table[@id="myTable"]/tr/td/text()') as $td)
{
/* 30 is each row's old-cell-count */
if($i%30==0) echo "<tr>\n";
$tds[]=$td->nodeValue;
/* 3 is each cell's old-cell-count */
if($i%3==2)
{
echo "\t<td>".implode("<br />",$tds)."</td>\n";
$tds=array();
}
if($i%30==29) echo "</tr>\n";
$i++;
}
echo "</table>";
Both outputs:
<table>
<tr>
<td>info0.1<br />info0.2<br />info0.3</td>
<td>info1.1<br />info1.2<br />info1.3</td>
<td>info2.1<br />info2.2<br />info2.3</td>
<td>info3.1<br />info3.2<br />info3.3</td>
<td>info4.1<br />info4.2<br />info4.3</td>
<td>info5.1<br />info5.2<br />info5.3</td>
<td>info6.1<br />info6.2<br />info6.3</td>
<td>info7.1<br />info7.2<br />info7.3</td>
<td>info8.1<br />info8.2<br />info8.3</td>
<td>info9.1<br />info9.2<br />info9.3</td>
</tr>
<tr>
<td>info10.1<br />info10.2<br />info10.3</td>
<td>info11.1<br />info11.2<br />info11.3</td>
<!-- ... -->
<td>info97.1<br />info97.2<br />info97.3</td>
<td>info98.1<br />info98.2<br />info98.3</td>
<td>info99.1<br />info99.2<br />info99.3</td>
</tr>
</table>