https://github.com/alexskrypnyk/csvtable
PHP class to work with CSV as a table and export it as Markdown.
https://github.com/alexskrypnyk/csvtable
converter csv formatter markdown table
Last synced: about 1 year ago
JSON representation
PHP class to work with CSV as a table and export it as Markdown.
- Host: GitHub
- URL: https://github.com/alexskrypnyk/csvtable
- Owner: AlexSkrypnyk
- License: gpl-3.0
- Created: 2023-05-20T00:52:33.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2025-02-07T08:54:31.000Z (about 1 year ago)
- Last Synced: 2025-03-18T16:14:18.902Z (about 1 year ago)
- Topics: converter, csv, formatter, markdown, table
- Language: PHP
- Homepage:
- Size: 52.7 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
PHP class to parse and format CSV content
[](https://github.com/AlexSkrypnyk/CsvTable/issues)
[](https://github.com/AlexSkrypnyk/CsvTable/pulls)
[](https://github.com/AlexSkrypnyk/CsvTable/actions/workflows/test-php.yml)
[](https://codecov.io/gh/AlexSkrypnyk/CsvTable)



---
## Features
- Single-file class to manipulate CSV table.
- Formatters for CSV, text table and Markdown table.
- Support for a custom formatter.
## Installation
```bash
composer require alexskrypnyk/csvtable
```
## Usage
Given a CSV file with the following content:
```csv
col11,col12,col13
col21,col22,col23
col31,col32,col33
```
### From string
```php
$csv = file_get_contents($csv_file);
// Format using the default formatter.
print (new CsvTable($csv))->format();
```
will produce identical CSV content by default:
```csv
col11,col12,col13
col21,col22,col23
col31,col32,col33
```
### From file
```php
print (CsvTable::fromFile($file))->format();
```
will produce identical CSV content by default:
```csv
col11,col12,col13
col21,col22,col23
col31,col32,col33
```
### Using `text_table` formatter
```php
print (CsvTable::fromFile($file))->format('text_table');
```
will produce table content:
```csv
col11|col12|col13
-----------------
col21|col22|col23
col31|col32|col33
```
### Using `text_table` formatter without a header
```php
print (CsvTable::fromFile($file))->withoutHeader()->format('text_table');
```
will produce table content:
```csv
col11|col12|col13
col21|col22|col23
col31|col32|col33
```
### Using `markdown_table` formatter
```php
print (CsvTable::fromFile($file))->format('markdown_table');
```
will produce Markdown table:
```markdown
| col11 | col12 | col13 |
|-------|-------|-------|
| col21 | col22 | col23 |
| col31 | col32 | col33 |
```
### Custom formatter as an anonymous callback
```php
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:
```csv
col11|col12|col13
=================
col21|col22|col23
col31|col32|col33
```
### Custom formatter as a class with default `format` method
```php
print (CsvTable::fromFile($file))->withoutHeader()->format(CustomFormatter::class);
```
### Custom formatter as a class with a custom method and options
```php
$formatter_options = ['option1' => 'value1', 'option2' => 'value2'];
print (CsvTable::fromFile($file))->withoutHeader()->format([CustomFormatter::class, 'customFormat'], $formatter_options);
```
## Maintenance
```bash
composer install
composer lint
composer test
```
---
_This repository was created using the [Scaffold](https://getscaffold.dev/) project template_
