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 is0).$length— The number of characters to include (optional). Ifnull, returns everything from$startto 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
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-04