PHPackages                             lostcause/php-htmltruncator - 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. lostcause/php-htmltruncator

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

lostcause/php-htmltruncator
===========================

HTML Truncation library, ported from the html\_truncator rubygem

1.0.6(11y ago)019MITPHPPHP &gt;=5.3.0

Since Mar 3Pushed 10y ago1 watchersCompare

[ Source](https://github.com/lostcause/php-htmltruncator)[ Packagist](https://packagist.org/packages/lostcause/php-htmltruncator)[ Docs](https://github.com/judev/php-htmltruncator)[ RSS](/packages/lostcause-php-htmltruncator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (8)Used By (0)

PHP HTML Truncator
==================

[](#php-html-truncator)

This is a PHP port of the [html\_truncator gem](https://github.com/nono/HTML-Truncator).

It will cleanly truncate HTML content, appending an ellipsis or other marker in the most suitable place. There is optional support for [php-html5lib](https://github.com/electrolinux/php-html5lib) if parsing of non-well-formed HTML5 is required. This is because PHP's DOMDocument-&gt;loadHTML (or the underlying libxml) doesn't recognize HTML5.

During the truncation process the HTML will be loaded into a DOMDocument. This can be relatively slow and memory hungry, so you may want to cache the truncated output.

How to use it
-------------

[](#how-to-use-it)

The HtmlTruncator\\Truncator class has only one static method, `truncate`, with 3 arguments:

- the HTML-formatted string to truncate
- the number of words to keep (real words, tags and attributes aren't counted)
- some options like the ellipsis (optional, '…' by default). If the option is string it is used as the ellipsis

And a static attribute, `$ellipsable_tags`, which lists the tags that can contain the ellipsis (by default: p ol ul li div header article nav section footer aside dd dt dl).

Examples
--------

[](#examples)

A simple example:

```
Truncator::truncate("Lorem ipsum dolor sit amet.", 3);
# => "Lorem ipsum dolor…"

```

If the text is too short to be truncated, it won't be modified:

```
Truncator::truncate("Lorem ipsum dolor sit amet.", 5);
# => "Lorem ipsum dolor sit amet."

```

If you prefer, you can have the length in characters instead of words:

```
Truncator::truncate("Lorem ipsum dolor sit amet.", 12, array('length_in_chars' => true));
# => "Lorem ipsum …"

```

You can customize the ellipsis:

```
Truncator::truncate("Lorem ipsum dolor sit amet.", 3, " (truncated)");
# => "Lorem ipsum dolor (truncated)"

```

And even have HTML in the ellipsis:

```
Truncator::truncate("Lorem ipsum dolor sit amet.", 3, '...');
# => "Lorem ipsum dolor..."

```

The ellipsis is put at the right place, inside ``, but not ``:

```
Truncator::truncate("Lorem ipsum dolor sit amet.", 3);
# => "Lorem ipsum dolor…"

```

You can indicate that a tag can contain the ellipsis by adding it to the ellipsable\_tags:

```
Truncator::$ellipsable_tags[] = "blockquote";
Truncator::truncate("Lorem ipsum dolor sit amet.", 3);
# => "Lorem ipsum dolor…"

```

Credits
-------

[](#credits)

This PHP port is entirely based on [Bruno Michel's excellent rubygem](https://github.com/nono/HTML-Truncator), any bugs introduced are likely mine.

Ported 2013-07-04 by Jude Venn , released under the MIT license

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 91.3% 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.

###  Release Activity

Cadence

Every ~65 days

Recently: every ~4 days

Total

7

Last Release

4071d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2c8c1371eb657c386d07e0b7a073ab52af9277ba82a99e3d23d13a6614692bf0?d=identicon)[lostcause](/maintainers/lostcause)

---

Top Contributors

[![judev](https://avatars.githubusercontent.com/u/219978?v=4)](https://github.com/judev "judev (21 commits)")[![patkruk](https://avatars.githubusercontent.com/u/1271901?v=4)](https://github.com/patkruk "patkruk (2 commits)")

---

Tags

phphtml

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/lostcause-php-htmltruncator/health.svg)

```
[![Health](https://phpackages.com/badges/lostcause-php-htmltruncator/health.svg)](https://phpackages.com/packages/lostcause-php-htmltruncator)
```

###  Alternatives

[christianklisch/slim-minify

Minify html output in slim framework

3831.9k1](/packages/christianklisch-slim-minify)[artem_c/emmet

emmet implementation for php

141.8k](/packages/artem-c-emmet)

PHPackages © 2026

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