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/>'; }
P粉4642089372024-04-02 19:49:46
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
JOHN DOE 12345678 00/00/0000 '; $dom = new DOMDocument(); $dom->loadHTML($string); $tr = $dom->getElementsByTagName('tr'); echo $tr->item(0)->nodeValue; lANE DOE 7234567890 30/00/0000
If you want to output all items, you can simply use a loop
P粉5462579132024-04-02 12:23:08
Where do you expect name
, number
and date
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 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. must be: 0 = name, 1 = number, 2 = date.
//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.'
';
}
}