$colLengths[$col]) { $colLengths[$col] = $len; } } } } $hr = str_repeat('-', array_sum($colLengths) + (count($colLengths) * 3) + 1); $output = $hr . "\n"; for ($row = 0; $row < count($table); $row++) { $colHeight = 0; for ($col = 0; $col < count($table[$row]); $col++) { $height = substr_count($table[$row][$col], "\n"); if ($height > $colHeight) { $colHeight = $height; } } for ($colRow = 0; $colRow <= $colHeight; $colRow++) { for ($col = 0; $col < count($table[$row]); $col++) { $colRows = explode("\n", $table[$row][$col]); $output .= '| ' . str_pad(isset($colRows[$colRow]) ? $colRows[$colRow] : '', $colLengths[$col], ' ', STR_PAD_RIGHT) . ' '; } $output .= "|\n"; } if ($row === 0) { $output .= $hr . "\n"; } } return trim($output . (count($table) > 1 ? $hr : '')); } }