Download the PHP package sabat24/markdown-table without Composer
On this page you can find all versions of the php package sabat24/markdown-table. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package markdown-table
markdown-table
Generate a markdown (GFM) table in PHP.
Contents
- What is this?
- When should I use this?
- Install
- Use
- API
- Table Class
- Column Class
- Options
- Compatibility
What is this?
This is a simple package that takes table data and generates a GitHub flavored markdown (GFM) table in PHP.
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
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.
Install
In PHP projects, install with Composer:
Use
Typical usage (defaults to align left):
Yields:
With align:
Yields:
With automatic headers:
Yields:
With custom string length function:
With allowed HTML tags:
Yields:
API
Table Class
__construct(array $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
Adds a column to the table at the specified position.
getColumn(int|string $pos): Column
Retrieves a column at the specified position.
clearColumns(): Table
Removes all columns from the table.
hasColumn(int|string $pos): bool
Checks if a column exists at the specified position.
hasColumns(): bool
Checks if the table has any columns defined.
dropColumn(int|string $pos): Table
Removes a column at the specified position.
setStringLengthFunction(callable $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
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
Sets configuration options for the table.
getOptions(): array
Gets current configuration options.
getString(array $rows): string
Generates a Markdown table string from the given rows.
Column Class
__construct(string $title, ?int $alignment = null)
Creates a new column with the specified title and optional alignment.
setAlignmentFromString(?string $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
The following options can be passed to the Table constructor or setOptions() method:
alignDelimiters (bool, default: true)
Whether to align the delimiters. When true, they are aligned; when false, they are staggered.
padding (bool, default: true)
Whether to add a space of padding between delimiters and cells.
delimiterStart (bool, default: true)
Whether to begin each row with the delimiter.
delimiterEnd (bool, default: true)
Whether to end each row with the delimiter.
autoHeaders (bool, default: false)
Whether to use the first row of data as headers.
headerSeparatorPadding (bool, default: false)
Whether to add padding spaces in the header separator row.
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
This package requires PHP 8.2 or higher.
All versions of markdown-table with dependencies
ext-mbstring Version *