I'm trying to format a NodeList obtained by getElementsByTagName and I can actually get the content of each tag, but I'm not able to filter, I'm trying to make the output like this:
EXAMPLE:
name: jhon doe
number: 12345678
date: 00/00/0000
but I only get the normal content:
JOHN DOE
12345678
00/00/0000
lane DOE
7234567890
00/30/0000
or if I use [0] it returns only the first letter/number of each tag.
J
1
0
l
7
3
my current code is below, any tips on what i can do?
<?php
$string = '
<tbody>
<tr>
<td>JOHN DOE</td>
<td style="background-color: rgb(25, 194, 25);">12345678</td>
<td>00/00/0000</td>
</tr>
<tr>
<td>lANE DOE</td>
<td style="background-color: rgb(25, 194, 25);">7234567890</td>
<td>30/00/0000</td>
</tr>
</tbody>';
$dom = new DOMDocument();
$dom->loadHTML($string);
foreach($dom->getElementsByTagName('td') as $td) {
echo $td->textContent[0] . '<br/>';
}
Where do you expect the name
,number
and date
to come from? PHP doesn't know what the table-values mean, so you have to set them yourself somehow.
There is no indication in the HTML what each table cell means, so you can only guess and hope the table structure never changes. The tables are ordered name - number - date, so you can deduct from the cell number what the label for a particular <td>
has to be: 0 = name, 1 = number, 2 = date.
So if you parse the HTML per table row and after that per table cell for each row, you can add a label based on the cell order.
But mind, if the content of the HTML is from an outside source and they change the order of the cells, it goes wrong.
//create an array of labels, based on the cell order per row
$labels=[
0=>'name',
1=>'number',
2=>'date'
];
$dom = new DOMDocument();
$dom->loadHTML($string);
// search for table ROWS
$table_rows = $dom->getElementsByTagName('tr');
//loop the ROWS
foreach($table_rows as $row){
//per ROW node, search for table CELLS
$row_cells = $row->getElementsByTagName('td');
//loop the CELLS
foreach($row_cells as $number => $cell){
//echo a label based on the cell order + the contents of the cell
echo $labels[$number].' - '.$cell->textContent.'<br>';
}
}