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

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

judev/php-htmltruncator
=======================

HTML Truncation library, ported from the html\_truncator rubygem

1.2.2(6y ago)26286.5k↓34.2%6[3 issues](https://github.com/judev/php-htmltruncator/issues)5MITPHPPHP &gt;=5.6.0CI failing

Since Mar 3Pushed 6y ago2 watchersCompare

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

READMEChangelogDependencies (2)Versions (11)Used By (5)

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

[](#php-html-truncator)

[![Build Status](https://camo.githubusercontent.com/aacc1105c3998160330e3e7be64db984474a4edb27220724819b3af93241f770/68747470733a2f2f6170692e7472617669732d63692e6f72672f6a756465762f7068702d68746d6c7472756e6361746f722e737667)](https://travis-ci.org/judev/php-htmltruncator)

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 [html5-php](https://github.com/Masterminds/html5-php) 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.

The [intl](http://php.net/intl) and [mbstring](http://php.net/mbstring) extensions are highly recommended to make truncation of international text more predictable.

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

40

—

FairBetter than 88% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity45

Moderate usage in the ecosystem

Community20

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 93.9% 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 ~232 days

Recently: every ~424 days

Total

10

Last Release

2373d ago

PHP version history (2 changes)1.0.0PHP &gt;=5.3.0

1.2.2PHP &gt;=5.6.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/0fab33e13c199cf3613f285d8f2de2487d58cd35afa0bea4ee3497438677517e?d=identicon)[judev](/maintainers/judev)

---

Top Contributors

[![judev](https://avatars.githubusercontent.com/u/219978?v=4)](https://github.com/judev "judev (31 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/judev-php-htmltruncator/health.svg)

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

###  Alternatives

[j0k3r/php-readability

Automatic article extraction from HTML

186808.8k6](/packages/j0k3r-php-readability)[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)
