- Single-file class to manipulate CSV table.
- Formatters for CSV, text table and Markdown table.
- Support for a custom formatter.
composer require alexskrypnyk/csvtable
Given a CSV file with the following content:
col11,col12,col13
col21,col22,col23
col31,col32,col33
$csv = file_get_contents($csv_file);
// Format using the default formatter.
print (new CsvTable($csv))->format();
will produce identical CSV content by default:
col11,col12,col13
col21,col22,col23
col31,col32,col33
print (CsvTable::fromFile($file))->format();
will produce identical CSV content by default:
col11,col12,col13
col21,col22,col23
col31,col32,col33
print (CsvTable::fromFile($file))->format('text_table');
will produce table content:
col11|col12|col13
-----------------
col21|col22|col23
col31|col32|col33
print (CsvTable::fromFile($file))->withoutHeader()->format('text_table');
will produce table content:
col11|col12|col13
col21|col22|col23
col31|col32|col33
print (CsvTable::fromFile($file))->format('markdown_table');
will produce Markdown table:
| col11 | col12 | col13 |
|-------|-------|-------|
| col21 | col22 | col23 |
| col31 | col32 | col33 |
print (CsvTable::fromFile($file))->format(function ($header, $rows, $options) {
$output = '';
if (count($header) > 0) {
$output = implode('|', $header);
$output .= "\n" . str_repeat('=', strlen($output)) . "\n";
}
return $output . implode("\n", array_map(static function ($row): string {
return implode('|', $row);
}, $rows));
});
will produce CSV content:
col11|col12|col13
=================
col21|col22|col23
col31|col32|col33
print (CsvTable::fromFile($file))->withoutHeader()->format(CustomFormatter::class);
$formatter_options = ['option1' => 'value1', 'option2' => 'value2'];
print (CsvTable::fromFile($file))->withoutHeader()->format([CustomFormatter::class, 'customFormat'], $formatter_options);
composer install
composer lint
composer test
This repository was created using the Scaffold project template