Вы должны извлечь строки вместо столбцов (без /td
в конце), а затем просто поместить все в таблицу HTML, с одним <tr>
для каждой строки:
<?php
// your current code
$xp = new DOMXPath($dom);
$rows = $xp->query('//table[@class="grid"]/tr[@class="belowHeader"]');
?>
<table>
<tbody>
<?php foreach ($rows as $row): ?>
<tr>
<?php foreach ($row->childNodes as $col): ?>
<?php if ($col->getAttribute('style') !== 'display:none'): ?>
<?php foreach ($col->childNodes as $colPart): ?>
<?php if ($colText = trim($colPart->textContent)): ?>
<td><?= $colText ?></td>
<?php elseif ($colPart instanceof DOMElement && $colPart->tagName === 'a'): ?>
<?php
$href = $colPart->getAttribute('href');
if (strpos($href, 'javascript') !== 0):
?>
<td><?= $colPart->getAttribute('href') ?></td>
<?php endif ?>
<?php endif ?>
<?php endforeach ?>
<?php endif ?>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</tbody>
</table>