How can wordwrap() be effectively used in PHP to prevent a table from extending excessively to the right?
When creating tables in PHP, the content within each cell may be too long and cause the table to extend excessively to the right, making it difficult to view. To prevent this issue, the wordwrap() function can be used to limit the length of the content within each cell by breaking it into multiple lines. By setting a maximum width for each cell, the table will not extend excessively to the right.
// Example PHP code snippet using wordwrap() to prevent table from extending excessively to the right
echo "<table>";
$data = array(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
);
foreach ($data as $row) {
echo "<tr>";
echo "<td>" . wordwrap($row, 30, "<br>") . "</td>"; // Limiting content to 30 characters per line
echo "</tr>";
}
echo "</table>";