3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = ' <table id="myTable"> <tbody> <tr> <td>08/20/18</td> <td> <a href="https://example.com/1a">Text 1 A</a> </td> <td> <a href="https://example.com/1b">Test 1 B</a> </td> </tr> <tr> <td>08/21/18</td> <td> <a href="https://example.com/2a">Text 2 A</a> </td> <td> <a href="https://example.com/2b">Test 2 B</a> </td> </tr> </tbody> </table> '; $data = []; $doc = new DOMDocument(); $doc->loadHTML('<?xml encoding="utf-8" ?>' . $html); $xpath = new DOMXPath($doc); $trs = $xpath->query("//*[@id='myTable']/tbody/tr"); foreach ($trs as $i => $tr) { /** @var DOMElement $td */ foreach ($tr->childNodes as $td) { if ($td instanceof DOMElement) { $row = []; /** @var DOMElement $a */ foreach ($td->childNodes as $a) { /** @var DOMAttr $attribute */ foreach ($a->attributes as $attribute) { $row[$attribute->name] = $attribute->value; } $row['content'] = $td->nodeValue; } $data[$i][] = $row; } } } print_r($data);

preferences:
162.2 ms | 404 KiB | 5 Q