定制 gherardobertini/truncate-html 二次开发

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

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

gherardobertini/truncate-html

Composer 安装命令:

composer require gherardobertini/truncate-html

包简介

Truncate HTML strings keeping valid tags without truncating words

README 文档

README

A lightweight PHP library to safely truncate HTML strings without breaking tags or cutting words.

📦 Installation

Install via Composer:

composer require gherardobertini/truncate-html

🚀 Usage

use Gherardobertini\TruncateHtml\TruncateHtml;

$html = "<p>Hello this is a test</p>";

echo TruncateHtml::truncate($html, 0, 10);
// Output: <p>Hello this</p>

echo TruncateHtml::truncate($html, 10);
// Output: <p>is a test</p>

🔧 Method Signature

public static function truncate(string $html, int $start = 0, ?int $length = null): string
  • $start — The character offset where to begin (default is 0).
  • $length — The number of characters to include (optional). If null, returns everything from $start to the end.
  • Words are never cut in half.
  • Open tags are properly closed to maintain valid HTML.

✅ Example

$html = "<p>Hello this is a test</p>";

TruncateHtml::truncate($html, 0, 10);  // <p>Hello this</p>
TruncateHtml::truncate($html, 10);     // <p>is a test</p>

🧪 Running Tests

Install dependencies and run PHPUnit:

composer install
vendor/bin/phpunit

📄 License

MIT License – see the LICENSE file for details.

统计信息

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

GitHub 信息

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

其他信息

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