定制 md-php/html-truncation 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

md-php/html-truncation

Composer 安装命令:

composer require md-php/html-truncation

包简介

Provides contract and implementation to truncate HTML code

README 文档

README

md-php/html-truncation is a PHP component that provides contract and implementation to truncate HTML code.

Powered on top of libxml, dom and mbstring builtin PHP extensions.

Text truncation is the process of shortening a piece of text by cutting it off after a certain length, typically adding an ellipsis ... or another indicator to show that the content has been clipped.

It's useful to create snippets showing only relevant context around a keyword and especially useful to limit content of items in list, for example: generic entry content in admin list, blog articles content and so on.

Architecture overview

Architecture overview class diagram

Features

  • Modern. SOLID & PSR compatible / linted with strict PHPStan and Psalm
  • Fast. Recursion-free algorithm
  • Memory-efficient. Supports huge documents with nesting up to 100,000 elements (optionally)
  • Fresh. Created in 2026, supports latest PHP versions (7.1+ and 8.0+)

Install

composer require md-php/html-truncation

Usage

$truncateHtml = new \MD\HTML\Action\Truncate(allowHugeDocument: false);
$truncateHtml->withTextLength(
    html: '<p>some very long html code</p>',
    maxLength: 10,
    ending: '...'5
);  # ... will return: '<p>some very ...</p>'

Read documentation for more examples.

Integration with Twig

Take a look for a md-php/bridge-twig-html-truncation component that provides additional Twig filter and function to truncate HTML code, like:

{{ "<p>some long html code</p>"|truncate_html_with_text_length(4) }}

or

{{ "some long markdown text"|markdown|raw|truncate_html_with_text_length(8, ';;;') }}

Documentation

Read documentation with examples: https://development.md.land/php/md-html-truncation/

Development

Initial setup:

cp .env.dist .env  # ... change `.env` for your needs
cp phpunit.php71.xml.dist phpunit.xml  # ... choose either 71 or 80 for your needs

Run checks (Psalm + PHPStan + PHPUnit):

docker compose run --build --remove-orphans --entrypoint='/usr/src/app/vendor/bin/deptrac analyse --config-file=deptrac.yaml' run-deptrac

for PHP_VERSION in '71' '80' '85'; do
  export PHP_VERSION="$PHP_VERSION"
  docker compose run -v "$PWD/vendor${PHP_VERSION}:/app/vendor" --build --remove-orphans --env-from-file .env -e XDEBUG_MODE=coverage --entrypoint="bash -c 'composer install ; mv composer.lock composer.php$PHP_VERSION.lock'" run-tests
  docker compose run -v "$PWD/vendor${PHP_VERSION}:/app/vendor" --build --remove-orphans --env-from-file .env -e XDEBUG_MODE=coverage --entrypoint="/usr/src/app/vendor/bin/phpunit --configuration /usr/src/app/phpunit.php$(( PHP_VERSION == 85 ? 80 : PHP_VERSION )).xml.dist --coverage-text" run-tests
  docker run -e "PHP_VERSION=$PHP_VERSION" -v "$PWD:/app" -v "$PWD/vendor${PHP_VERSION}:/app/vendor" --rm -it ghcr.io/danog/psalm:latest /composer/vendor/bin/psalm --no-cache
  docker run -e "PHP_VERSION=$PHP_VERSION" -v "$PWD:/app" -v "$PWD/vendor${PHP_VERSION}:/app/vendor" --rm -it ghcr.io/phpstan/phpstan analyse --configuration phpstan.neon
done;

Validate files:

composer validate --strict
find ./src ./tests -name "*.php" -exec php -l {} \;

Comparison with other solutions

  • md-php/html-truncation (THIS)
    • Method: libxml ✅
    • Stars: N/A
    • Installs: N/A
    • Status: Last update: 2026-03-25
    • Features: (see features)
    • PHP version: ^7.1 or ^8.0
  • urodoz/truncate-html
    • Method: regular expression ❗
    • Stars: 21
    • Installs: 287,129
    • Status:
      Last update: 2014-05-25 ❗
      Abandoned (De-facto): No updates, no activity on issues. ❗
    • PHP version: >=5.3
    • Cons:
      • Coupled to Twig component ❗️
  • bluetel/twig-truncate-extension
    • Method: libxml ✅
    • Stars: 11
    • Installs: 97,376
    • Status:
      Last update: 2016-01-19 ❗
      Abandoned (De-facto): No updates, no activity on issues. ❗
    • PHP version: >=5.3
    • Cons:
      • issues in master branch ❗
        • dumps result only as HTML as full document, no fragment support.
      • recursion method ❗
      • Coupled to Twig component ❗
  • judev/php-htmltruncator
    • Method: libxml ✅
    • Stars: 29
    • Installs: 271,305
    • Status:
      Last update: 2019-11-19 ❗
      Abandoned (no updates, no activity on issues) ❗
    • PHP version: >=5.6
    • Cons
      • One class per file principle violation ❗️
  • dzango/TwigTruncateExtension
    • Method: regular expression ❗️
    • Stars: 13
    • Installs: 236,921
    • Status:
      Last update: 2017-12-02 ❗️
      Abandoned (no updates, no activity on issues) ❗
    • PHP version: >=5.6
    • Pros:
      • Adjust cut off in the middle of the word ✅
    • Cons:
      • One class per file principle violation ❗️
      • Coupled to Twig component ❗️

Roadmap

  • add cut off in the middle of the word adjustment
  • add feature to truncate text by the words count
  • add feature to exclude elements from text truncation
  • add benchmarks
  • simplify Dockerfile

Changelog

License (MIT)

md-php/html-truncation 适用场景与选型建议

md-php/html-truncation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「html」 「truncate」 「truncation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 md-php/html-truncation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 md-php/html-truncation 我们能提供哪些服务?
定制开发 / 二次开发

基于 md-php/html-truncation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 25
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 30
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-21