I'm trying to format a NodeList obtained via getElementsByTagName, actually I can get the content of each tag, but I can't filter, I'm trying to make the output like this:
EXAMPLE: name: jhon doe number: 12345678 date: 00/00/0000
But I only get normal content:
JOHN DOE 12345678 00/00/0000 lane DOE 7234567890 00/30/0000
Or if I use [0], it only returns 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/>'; }
The mistake you made is that you used the td tag, which does not return every record but every value (see your code).
First you should use the "tr" tag
Secondly, you should use nodeValue to get the data for any specific item by mentioning the index
The corrected code is given for your reference. If you have any questions, please feel free to ask questions
If you want to output all items, you can simply use a loop
Where do you expect
name
,number
anddate
to come from? PHP has no idea what the table values mean, so you have to set them yourself somehow.There is no indication in HTML what each table cell means, so you can only guess and hope that the table structure never changes. The tables are sorted by name - number - date, so you can deduce from the cell numbers that the label for a specific must be: 0 = name, 1 = number, 2 = date.
So if you parse the HTML for each table row , and then parse each row for each table cell , you can add tags based on cell order.
But please note that if the content of the HTML comes from an external source and they change the order of the cells, an error will occur.