Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: 3 months 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 (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-07-04T08:33:58.000Z (6 months ago)
- Last Synced: 2024-10-11T01:41:13.061Z (3 months ago)
- Topics: converter, csv, formatter, markdown, table
- Language: PHP
- Homepage:
- Size: 37.1 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
CsvTable
[![GitHub Issues](https://img.shields.io/github/issues/AlexSkrypnyk/CsvTable.svg)](https://github.com/AlexSkrypnyk/CsvTable/issues)
[![GitHub Pull Requests](https://img.shields.io/github/issues-pr/AlexSkrypnyk/CsvTable.svg)](https://github.com/AlexSkrypnyk/CsvTable/pulls)
[![Test](https://github.com/AlexSkrypnyk/CsvTable/actions/workflows/test-php.yml/badge.svg)](https://github.com/AlexSkrypnyk/CsvTable/actions/workflows/test-php.yml)
[![codecov](https://codecov.io/gh/AlexSkrypnyk/CsvTable/graph/badge.svg?token=7WEB1IXBYT)](https://codecov.io/gh/AlexSkrypnyk/CsvTable)
![GitHub release (latest by date)](https://img.shields.io/github/v/release/AlexSkrypnyk/CsvTable)
![LICENSE](https://img.shields.io/github/license/AlexSkrypnyk/CsvTable)
![Renovate](https://img.shields.io/badge/renovate-enabled-green?logo=renovatebot)---
PHP class to work with CSV as a table and export it as Markdown.
## Features
- Single-file class to manipulate CSV table.
- Renderers for CSV and text table.
- Ability to provide custom renderer.## 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);
// Render using the default renderer.
print (new CsvTable($csv))->render();
```
will produce identical CSV content by default:
```csv
col11,col12,col13
col21,col22,col23
col31,col32,col33
```### From file
```php
print (CsvTable::fromFile($file))->render();
```
will produce identical CSV content by default:
```csv
col11,col12,col13
col21,col22,col23
col31,col32,col33
```### Using `CsvTable::renderTextTable()` renderer
```php
print (CsvTable::fromFile($file))->render([CsvTable::class, 'renderTextTable']);
```
will produce table content:
```csv
col11|col12|col13
-----------------
col21|col22|col23
col31|col32|col33
```### Using `CsvTable::renderTextTable()` renderer with disabled header
```php
print (CsvTable::fromFile($file))->noHeader()->render([CsvTable::class, 'renderTextTable']);
```
will produce table content:
```csv
col11|col12|col13
col21|col22|col23
col31|col32|col33
```### Custom renderer from class
```php
print (CsvTable::fromFile($file))->render(Markdown::class);
```
will produce Markdown content:
```markdown
| col11 | col12 | col13 |
|-------|-------|-------|
| col21 | col22 | col23 |
| col31 | col32 | col33 |
```### Custom renderer as a callback
```php
print (CsvTable::fromFile($file))->render(function ($header, $rows, $options) {
if (count($header) > 0) {
$header = implode('|', $header);
$header = $header . "\n" . str_repeat('-', strlen($header)) . "\n";
}
else {
$header = '';
}return $header . implode("\n", array_map(function ($row) {
return implode('|', $row);
}, $rows));
});
```
will produce CSV content:
```csv
col11|col12|col13
-----------------
col21|col22|col23
col31|col32|col33
```## Maintenance
```bash
composer install
composer lint
composer test
```
---
Repository created using https://getscaffold.dev/ project scaffold template