sabat24/markdown-table
Composer 安装命令:
composer require sabat24/markdown-table
包简介
Generate GitHub Flavored Markdown tables.
README 文档
README
Generate a markdown (GFM) table in PHP.
Contents
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:
composer require sabat24/markdown-table
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 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> |
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.
sabat24/markdown-table 适用场景与选型建议
sabat24/markdown-table 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 978 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 03 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「markdown」 「array」 「table」 「gfm」 「markdown-table」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sabat24/markdown-table 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sabat24/markdown-table 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sabat24/markdown-table 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
A set of useful PHP classes.
Trait providing methods to set class properties with an array.
Adds more BBCode
Traits to build collections of specific objects
This adds functions about array. If you feel like there few php built-in functions about array, this will be useful.
统计信息
- 总下载量: 978
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-29