air-petr / php-tabulator
Tabular data in PHP
v1.0.2
2022-02-10 19:09 UTC
Requires
- php: >=7.4
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-10-11 00:59:01 UTC
README
Pretty-print the tabular data in various ways.
Instalation
composer require ar-petr/php-tabulator
Usage
You will get a Tabulator
class with a static interface:
use AirPetr\Tabulator; $body = [ [1, 'Lila', 'Hevner'], [2, 'Florrie', 'Gravie'], ]; $header = ['id', 'first_name', 'last_name']; echo Tabulator::getPlain($body); // Table without headers echo Tabulator::getPlain($body, $header); // Table with headers
There are different types of output:
Plain
echo Tabulator::getPlain($data, $headers); /* id first_name last_name 1 Lila Hevner 2 Florrie Gravie */
Simple
echo Tabulator::getSimple($data, $headers); /* id first_name last_name -- ---------- --------- 1 Lila Hevner 2 Florrie Gravie */
GitHub
echo Tabulator::getGitHub($data, $headers); /* | id | first_name | last_name | | -- | ---------- | --------- | | 1 | Lila | Hevner | | 2 | Florrie | Gravie | */
More types may be added in the future.
Numbers
Columns with numbers are flushed to the left.
| ip_address | age | account |
| --------------- | ---- | -------- |
| 219.249.38.228 | 2362 | 12276.68 |
| 197.81.54.113 | 41 | 6496.03 |
| 176.111.139.6 | 64 | 3291.72 |
| 208.178.177.206 | 34 | 4311.57 |
Demo
You can see some examples by running scripts from a demo
folder:
php demo/plain.php
php demo/github.php
# etc.
Testing
Run tests by:
composer test