<?php
$html = '<html>
<body>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0> <TR> <TD BGCOLOR=000000> <TABLE BORDER=0 CELLSPACING=1 CELLPADDING=4> <TR STYLE="font-weight: 700;" ALIGN=CENTER BGCOLOR=99CCFF>
<td>Product</td>
<td>Quantity</td>
<td>Retail Price</td>
<td>Total Retail Price</td>
<td>UPC</td>
</tr>
<tr bgcolor="FFFFFF">
<td style="">Smith High-Water Chroma Pop+ Polar Chromic NXT Sunglasses, Black</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">$259.00</td>
<td style="text-align: right;">$259.00</td>
<td style=""></td>
</tr>
<tr bgcolor="FFFFFF">
<td style="">Guest 2613A Charge Pro Series Marine Battery Charger (12/24-Volt, 15-Amps 5/5/5, Triple Output)</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">$179.18</td>
<td style="text-align: right;">$179.18</td>
<td style=""></td>
</tr>
</table>
</body>
</html>';
$dom = new DOMDocument();
$dom->loadHTML($html);
// Grab first table from dom and store its rows and columns by tag name.
$rows = $dom->getElementsByTagName('table')->item(1)->getElementsByTagName('tr');
$headers = $rows->item(0)->getElementsByTagName('td');
$resultArray = [];
$keys = [];
foreach($headers as $header) {
$keys[] = strtolower($header->nodeValue);
}
print_r($keys);
foreach($rows as $row) {
$item = [];
foreach($keys as $i=>$key) {
$cells = $row->getElementsByTagName('td');
$item[$key] = $cells->item($i)->nodeValue;
}
$resultArray[] = $item;
}
print_r($resultArray);
- Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
- Array
(
[0] => product
[1] => quantity
[2] => retail price
[3] => total retail price
[4] => upc
)
Array
(
[0] => Array
(
[product] => Product
[quantity] => Quantity
[retail price] => Retail Price
[total retail price] => Total Retail Price
[upc] => UPC
)
[1] => Array
(
[product] => Smith High-Water Chroma Pop+ Polar Chromic NXT Sunglasses, Black
[quantity] => 1
[retail price] => $259.00
[total retail price] => $259.00
[upc] =>
)
[2] => Array
(
[product] => Guest 2613A Charge Pro Series Marine Battery Charger (12/24-Volt, 15-Amps 5/5/5, Triple Output)
[quantity] => 1
[retail price] => $179.18
[total retail price] => $179.18
[upc] =>
)
)
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Array
(
[0] => product
[1] => quantity
[2] => retail price
[3] => total retail price
[4] => upc
)
Array
(
[0] => Array
(
[product] => Product
[quantity] => Quantity
[retail price] => Retail Price
[total retail price] => Total Retail Price
[upc] => UPC
)
[1] => Array
(
[product] => Smith High-Water Chroma Pop+ Polar Chromic NXT Sunglasses, Black
[quantity] => 1
[retail price] => $259.00
[total retail price] => $259.00
[upc] =>
)
[2] => Array
(
[product] => Guest 2613A Charge Pro Series Marine Battery Charger (12/24-Volt, 15-Amps 5/5/5, Triple Output)
[quantity] => 1
[retail price] => $179.18
[total retail price] => $179.18
[upc] =>
)
)
preferences:
221.98 ms | 403 KiB | 287 Q