alexskrypnyk/csvtable

PHP class to parse and format CSV content.

Fund package maintenance!
Patreon

Installs: 15 660

Dependents: 3

Suggesters: 0

Security: 0

Stars: 3

Watchers: 2

Forks: 1

Open Issues: 1

pkg:composer/alexskrypnyk/csvtable

1.2.0 2025-12-04 05:05 UTC

README

Yourproject logo

PHP class to parse and format CSV content

GitHub Issues GitHub Pull Requests Test codecov GitHub release (latest by date) LICENSE Renovate

Features

  • Single-file class to manipulate CSV table.
  • Formatters for CSV, text table and Markdown table.
  • Support for a custom formatter.
  • Column manipulation: reorder, filter, and exclude columns.

Installation

composer require alexskrypnyk/csvtable

Usage

Given a CSV file with the following content:

col11,col12,col13
col21,col22,col23
col31,col32,col33      

From string

$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      

From file

print (CsvTable::fromFile($file))->format();

will produce identical CSV content by default:

col11,col12,col13
col21,col22,col23
col31,col32,col33

Using text_table formatter

print (CsvTable::fromFile($file))->format('text_table');

will produce table content:

col11|col12|col13
-----------------
col21|col22|col23
col31|col32|col33     

Using text_table formatter without a header

print (CsvTable::fromFile($file))->withoutHeader()->format('text_table');

will produce table content:

col11|col12|col13
col21|col22|col23
col31|col32|col33     

Using markdown_table formatter

print (CsvTable::fromFile($file))->format('markdown_table');

will produce Markdown table:

| col11 | col12 | col13 |
|-------|-------|-------|
| col21 | col22 | col23 |
| col31 | col32 | col33 |     

Custom formatter as an anonymous callback

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     

Custom formatter as a class with default format method

print (CsvTable::fromFile($file))->withoutHeader()->format(CustomFormatter::class);

Custom formatter as a class with a custom method and options

$formatter_options = ['option1' => 'value1', 'option2' => 'value2'];
print (CsvTable::fromFile($file))->withoutHeader()->format([CustomFormatter::class, 'customFormat'], $formatter_options);

Column Manipulation

Given a CSV file with the following content:

Name,Age,City,Country
John,30,New York,USA
Jane,25,London,UK

Reorder columns with columnOrder()

Reorder columns by specifying the desired order. Columns not specified are appended in their original order.

print (CsvTable::fromFile($file))->columnOrder(['City', 'Name'])->format('markdown_table');

will produce:

| City     | Name | Age | Country |
|----------|------|-----|---------|
| New York | John | 30  | USA     |
| London   | Jane | 25  | UK      |

Filter to specific columns with onlyColumns()

Keep only the specified columns in the output.

print (CsvTable::fromFile($file))->onlyColumns(['Name', 'City'])->format('markdown_table');

will produce:

| Name | City     |
|------|----------|
| John | New York |
| Jane | London   |

Exclude columns with withoutColumns()

Exclude specified columns from the output.

print (CsvTable::fromFile($file))->withoutColumns(['Age', 'Country'])->format('markdown_table');

will produce:

| Name | City     |
|------|----------|
| John | New York |
| Jane | London   |

Using column indices

All column methods accept both column names (strings) and zero-based indices (integers).

// Using indices
print (CsvTable::fromFile($file))->columnOrder([2, 0])->format();

// Using mixed names and indices
print (CsvTable::fromFile($file))->onlyColumns(['Name', 2])->format();

Combining column transformations

Column transformations are applied in order: onlyColumns()withoutColumns()columnOrder().

print (CsvTable::fromFile($file))
  ->withoutColumns(['Age'])
  ->columnOrder(['Country', 'City'])
  ->format('markdown_table');

will produce:

| Country | City     | Name |
|---------|----------|------|
| USA     | New York | John |
| UK      | London   | Jane |

Reset column transformations

$table = CsvTable::fromFile($file);

// Apply and use transformations
$table->columnOrder(['City', 'Name']);
print $table->format();

// Reset and format without transformations
print $table->resetColumns()->format();

// Individual reset methods are also available:
// $table->resetColumnOrder();
// $table->resetOnlyColumns();
// $table->resetWithoutColumns();

Maintenance

composer install
composer lint
composer test

This repository was created using the Scaffold project template