PHPackages                             gherardobertini/truncate-html - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. gherardobertini/truncate-html

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

gherardobertini/truncate-html
=============================

Truncate HTML strings keeping valid tags without truncating words

0720↓50%PHPCI passing

Since Apr 4Pushed 1y ago1 watchersCompare

[ Source](https://github.com/gherardobertini/truncate-html)[ Packagist](https://packagist.org/packages/gherardobertini/truncate-html)[ RSS](/packages/gherardobertini-truncate-html/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

truncate-html
=============

[](#truncate-html)

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

📦 Installation
--------------

[](#-installation)

Install via Composer:

```
composer require gherardobertini/truncate-html
```

🚀 Usage
-------

[](#-usage)

```
use Gherardobertini\TruncateHtml\TruncateHtml;

$html = "Hello this is a test";

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

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

### 🔧 Method Signature

[](#-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
---------

[](#-example)

```
$html = "Hello this is a test";

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

🧪 Running Tests
---------------

[](#-running-tests)

Install dependencies and run PHPUnit:

```
composer install
vendor/bin/phpunit
```

📄 License
---------

[](#-license)

MIT License – see the [LICENSE](LICENSE) file for details.

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity17

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2725360?v=4)[Gherardo Bertini](/maintainers/gherardobertini)[@gherardobertini](https://github.com/gherardobertini)

---

Top Contributors

[![gherardobertini](https://avatars.githubusercontent.com/u/2725360?v=4)](https://github.com/gherardobertini "gherardobertini (7 commits)")

### Embed Badge

![Health badge](/badges/gherardobertini-truncate-html/health.svg)

```
[![Health](https://phpackages.com/badges/gherardobertini-truncate-html/health.svg)](https://phpackages.com/packages/gherardobertini-truncate-html)
```

###  Alternatives

[guava/filament-icon-picker

A filament plugin that adds an icon picker field.

161531.9k25](/packages/guava-filament-icon-picker)[mdmsoft/yii2-gii

Yii2 custom generator for gii

1335.4k1](/packages/mdmsoft-yii2-gii)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
