PHPackages                             sabat24/markdown-table - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sabat24/markdown-table

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

sabat24/markdown-table
======================

Generate GitHub Flavored Markdown tables.

0.9.5-beta(1y ago)2961↓66.7%MITPHPPHP ^8.2

Since Mar 29Pushed 1y ago1 watchersCompare

[ Source](https://github.com/sabat24/markdown-table)[ Packagist](https://packagist.org/packages/sabat24/markdown-table)[ RSS](/packages/sabat24-markdown-table/feed)WikiDiscussions master Synced yesterday

READMEChangelog (6)Dependencies (2)Versions (7)Used By (0)

markdown-table
==============

[](#markdown-table)

Generate a markdown (GFM) table in PHP.

Contents
--------

[](#contents)

- [What is this?](#what-is-this)
- [When should I use this?](#when-should-i-use-this)
- [Install](#install)
- [Use](#use)
- [API](#api)
    - [Table Class](#table-class)
    - [Column Class](#column-class)
    - [Options](#options)
- [Compatibility](#compatibility)

What is this?
-------------

[](#what-is-this)

This is a simple package that takes table data and generates a [GitHub flavored markdown (GFM)](https://docs.github.com/en/github/writing-on-github/working-with-advanced-formatting/organizing-information-with-tables)table in PHP.

When should I use this?
-----------------------

[](#when-should-i-use-this)

You can use this package when you want to generate the source code of a GFM table from PHP data structures.

Inspiration
-----------

[](#inspiration)

This is a PHP implementation inspired by the JavaScript [wooorm/markdown-table](https://github.com/wooorm/markdown-table)package with similar API and functionality.

I also made it compatible with [the-kbA-team/markdown-table](https://github.com/the-kbA-team/markdown-table) because I use this package in some projects.

Install
-------

[](#install)

In PHP projects, install with [Composer](https://getcomposer.org/):

```
composer require sabat24/markdown-table
```

Use
---

[](#use)

Typical 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 lineSecond line'],
    ['Formatting', 'Bold text and italic text'],
]);
```

Yields:

```
| Feature     | Description                                         |
|-------------|-----------------------------------------------------|
| Line breaks | First lineSecond line                          |
| Formatting  | Bold text and italic text |
```

API
===

[](#api)

### Table Class

[](#table-class)

#### `__construct(array $columnNames = [], array $options = [], bool $useNamesAsPositions = false)`

[](#__constructarray-columnnames---array-options---bool-usenamesaspositions--false)

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)

#### `addColumn(int|string $pos, Column $column): Table`

[](#addcolumnintstring-pos-column-column-table)

Adds a column to the table at the specified position.

#### `getColumn(int|string $pos): Column`

[](#getcolumnintstring-pos-column)

Retrieves a column at the specified position.

#### `clearColumns(): Table`

[](#clearcolumns-table)

Removes all columns from the table.

#### `hasColumn(int|string $pos): bool`

[](#hascolumnintstring-pos-bool)

Checks if a column exists at the specified position.

#### `hasColumns(): bool`

[](#hascolumns-bool)

Checks if the table has any columns defined.

#### `dropColumn(int|string $pos): Table`

[](#dropcolumnintstring-pos-table)

Removes a column at the specified position.

#### `setStringLengthFunction(callable $callback): Table`

[](#setstringlengthfunctioncallable-callback-table)

Sets a custom function to determine the visual length of strings, useful for handling CJK characters and emoji.

#### `setAlignment(array|string $align): Table`

[](#setalignmentarraystring-align-table)

Sets alignment for columns. Accepts:

- Single string for all columns: 'l'/'left', 'r'/'right', 'c'/'center'
- Array of alignments for individual columns

#### `setOptions(array $options): Table`

[](#setoptionsarray-options-table)

Sets configuration options for the table.

#### `getOptions(): array`

[](#getoptions-array)

Gets current configuration options.

#### `getString(array $rows): string`

[](#getstringarray-rows-string)

Generates a Markdown table string from the given rows.

### Column Class

[](#column-class)

#### `__construct(string $title, ?int $alignment = null)`

[](#__constructstring-title-int-alignment--null)

Creates a new column with the specified title and optional alignment.

#### `setAlignmentFromString(?string $alignment): Column`

[](#setalignmentfromstringstring-alignment-column)

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

### Options

[](#options)

The following options can be passed to the `Table` constructor or `setOptions()` method:

##### `alignDelimiters` (bool, default: `true`)

[](#aligndelimiters-bool-default-true)

Whether to align the delimiters. When `true`, they are aligned; when `false`, they are staggered.

##### `padding` (bool, default: `true`)

[](#padding-bool-default-true)

Whether to add a space of padding between delimiters and cells.

##### `delimiterStart` (bool, default: `true`)

[](#delimiterstart-bool-default-true)

Whether to begin each row with the delimiter.

##### `delimiterEnd` (bool, default: `true`)

[](#delimiterend-bool-default-true)

Whether to end each row with the delimiter.

##### `autoHeaders` (bool, default: `false`)

[](#autoheaders-bool-default-false)

Whether to use the first row of data as headers.

##### `headerSeparatorPadding` (bool, default: `false`)

[](#headerseparatorpadding-bool-default-false)

Whether to add padding spaces in the header separator row.

##### `allowedTags` (array, default: `[]`)

[](#allowedtags-array-default-)

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.

Compatibility
-------------

[](#compatibility)

This package requires PHP 8.2 or higher.

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance43

Moderate activity, may be stable

Popularity21

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

6

Last Release

453d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/eabc4df48670115b6a2528a5b4ed846462332407aa4303b7ee9733da363ea53a?d=identicon)[sabat24](/maintainers/sabat24)

---

Top Contributors

[![sabat24](https://avatars.githubusercontent.com/u/6381661?v=4)](https://github.com/sabat24 "sabat24 (9 commits)")

---

Tags

gfmmarkdownmarkdown-tablephparraymarkdowngfmtablemarkdown-table

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/sabat24-markdown-table/health.svg)

```
[![Health](https://phpackages.com/badges/sabat24-markdown-table/health.svg)](https://phpackages.com/packages/sabat24-markdown-table)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
