How can PHP be used to extract content from an HTML table and format it with specific line breaks?
To extract content from an HTML table using PHP, you can use the DOMDocument class to parse the HTML and then loop through the table rows to extract the data. To format the content with specific line breaks, you can concatenate the extracted data with the desired line break character.
$html = '<table>
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
</tr>
<tr>
<td>Row 2, Column 1</td>
<td>Row 2, Column 2</td>
</tr>
</table>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$table = $dom->getElementsByTagName('table')->item(0);
$rows = $table->getElementsByTagName('tr');
foreach ($rows as $row) {
$cells = $row->getElementsByTagName('td');
foreach ($cells as $cell) {
echo $cell->nodeValue . "<br>";
}
echo "<br>";
}
Keywords
Related Questions
- How can the issue of receiving a blank page after clicking the "next question" button be resolved?
- What are some best practices for handling XML data in PHP to ensure efficient processing?
- How can using arrays for variable assignment in Smarty templates improve code readability and efficiency in PHP?