Generate a markdown (GFM) table in PHP.
This is a simple package that takes table data and generates a GitHub flavored markdown (GFM) table in PHP.
You can use this package when you want to generate the source code of a GFM table from PHP data structures.
This is a PHP implementation inspired by the JavaScript wooorm/markdown-table package with similar API and functionality.
I also made it compatible with the-kbA-team/markdown-table because I use this package in some projects.
In PHP projects, install with Composer:
composer require sabat24/markdown-tableTypical usage (defaults to align left):
use sabat24\MarkdownTable\Table;
$table = new Table(['Branch', 'Commit']);
echo $table->getString([
['main', '0123456789abcdef'],
['staging', 'fedcba9876543210'],
]);Yields:
| Branch | Commit |
|---------|------------------|
| main | 0123456789abcdef |
| staging | fedcba9876543210 |With align:
use sabat24\MarkdownTable\Table;
$table = new Table(['Beep', 'No.', 'Boop']);
$table->setAlignment(['l', 'c', 'r']);
echo $table->getString([
['beep', '1024', 'xyz'],
['boop', '3388450', 'tuv'],
['foo', '10106', 'qrstuv'],
['bar', '45', 'lmno'],
]);Yields:
| Beep | No. | Boop |
|:-----|:-------:|-------:|
| beep | 1024 | xyz |
| boop | 3388450 | tuv |
| foo | 10106 | qrstuv |
| bar | 45 | lmno |With automatic headers:
use sabat24\MarkdownTable\Table;
$table = new Table(options: ['autoHeaders' => true]);
echo $table->getString([
['Col.A', 'Col.B', 'Col.C'],
['a', 'z', ''],
['b', '', ''],
['c', 'y', 'x'],
]);Yields:
| Col.A | Col.B | Col.C |
|-------|-------|-------|
| a | z | |
| b | | |
| c | y | x |With custom string length function:
use sabat24\MarkdownTable\Table;
// Using a custom function for handling special characters like emoji properly
function stringWidth($string): int
{
// This is a simplified example - in production, you might want
// a more sophisticated library that handles all Unicode properties
$pattern = '/[\p{East_Asian_Width=F}\p{East_Asian_Width=W}]/u';
$wide = preg_match_all($pattern, $string, $matches);
return mb_strlen($string) + $wide;
}
$table = new Table(['Alpha', 'Bravo']);
$table->setStringLengthFunction('stringWidth');
echo $table->getString([
['中文', 'Charlie'],
['👩❤️👩', 'Delta'],
]);With allowed HTML tags:
use sabat24\MarkdownTable\Table;
$table = new Table(['Feature', 'Description']);
$table->setOptions(['allowedTags' => ['br', 'strong', 'em']]);
echo $table->getString([
['Line breaks', 'First line<br/>Second line'],
['Formatting', '<strong>Bold text</strong> and <em>italic text</em>'],
]);Yields:
| Feature | Description |
|-------------|-----------------------------------------------------|
| Line breaks | First line<br/>Second line |
| Formatting | <strong>Bold text</strong> and <em>italic text</em> |Creates a new table with:
$columnNames: Optional array of column names$options: Optional configuration options$useNamesAsPositions: When true, uses column names as position identifiers instead of array keys (default: false)
Adds a column to the table at the specified position.
Retrieves a column at the specified position.
Removes all columns from the table.
Checks if a column exists at the specified position.
Checks if the table has any columns defined.
Removes a column at the specified position.
Sets a custom function to determine the visual length of strings, useful for handling CJK characters and emoji.
Sets alignment for columns. Accepts:
- Single string for all columns: 'l'/'left', 'r'/'right', 'c'/'center'
- Array of alignments for individual columns
Sets configuration options for the table.
Gets current configuration options.
Generates a Markdown table string from the given rows.
Creates a new column with the specified title and optional alignment.
Sets the column alignment using a string:
- 'l' or 'left' for left alignment
- 'r' or 'right' for right alignment
- 'c' or 'center' for center alignment
The following options can be passed to the Table constructor or setOptions() method:
Whether to align the delimiters. When true, they are aligned; when false, they are staggered.
Whether to add a space of padding between delimiters and cells.
Whether to begin each row with the delimiter.
Whether to end each row with the delimiter.
Whether to use the first row of data as headers.
Whether to add padding spaces in the header separator row.
An array of HTML tags that should be preserved in the output. By default, all HTML is escaped, but you can specify tags like ['br', 'strong', 'em'] to allow these tags to remain unescaped in the generated table.
This package requires PHP 8.2 or higher.