OK, jako że mi się nudzi to zrobiłem coś na szybko.
Plik klasy (TableASCII.php):
<?php
class TableASCII {
private $data;
private $columnsWidths;
public function __construct() {
$this->columnsWidths = array(); }
public function addRow() {
$column = 0;
$this->data[$this->getRowsNumber()] = array(); $this->data[$this->getRowsNumber() - 1][$column] = $element;
if($length > $this->columnsWidths[$column]) {
$this->columnsWidths[$column] = $length;
}
}
else {
$this->columnsWidths[$column] = $length;
}
++$column;
}
}
public function getRowsNumber() {
return count($this->data); }
public function getColumnsNumber() {
return count($this->columnsWidths); }
public function getElement($row, $column) {
return $this->data[$row][$column];
}
public function output($fileName = null) {
$horizontalBorder = str_repeat('-', array_sum($this->columnsWidths) + $this->getColumnsNumber() + 1
); $output = $horizontalBorder;
foreach($this->data as $row) {
$output .= "\r\n" . '|';
$currentColumn = 0;
foreach($row as $element) {
$output .= $element . ($length < $this->getColumnWidth($currentColumn) ?
str_repeat(' ', $this->getColumnWidth($currentColumn) - $length) : '') . '|'; ++$currentColumn;
}
}
$output .= "\r\n" . $horizontalBorder;
$file = fopen($fileName, 'w'); }
return $output;
}
private function getColumnWidth($index) {
return $this->columnsWidths[$index];
}
}
?>
I przykład użycia:
<?php
header('Content-type: text/plain');
require_once('TableASCII.php');
$table = new TableASCII();
$table->addRow('Tata', 'Muminka', 63);
$table->addRow('Mała', 'Mi', 15);
$table->addRow('Buuuuuuuuuuuuuuuuuka', 'Zła', 78423);
$table->addRow('Paszczak', 'Paszczakowski', 75);
echo $table->output('muminkowo.txt');
?>
Klasa pozwala wypisywać tabelkę i opcjonalnie zapisywać ją do pliku. Powinna zabezpieczać się przez nieprawidłowymi liczbami kolumn itd., ale tego już charytatywnie nie mogę zrobić