$colLengths[$col]) { $colLengths[$col] = $len; } } } } $totalWidth = array_sum($colLengths) + (count($colLengths) * 3) + 1; $hr = str_repeat('-', $totalWidth); $output = $hr . "\n"; for ($row = 0; $row < count($table); $row++) { if (is_string($table[$row])) { //Special handling to show a sub-header/divider if ($row > 1) { $output .= $hr . "\n"; } $output .= '| ' . str_pad($table[$row], $totalWidth - 4, ' ', STR_PAD_BOTH) . ' ' . "|\n"; $output .= $hr . "\n"; continue; } $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 : '')); } }