PHPackages                             bohacpetr/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. bohacpetr/truncate-html

ActiveLibrary

bohacpetr/truncate-html
=======================

Handle truncate action on HTML strings

1.1.0(6y ago)033.9k↑38%MITPHPPHP &gt;=5.3.3

Since May 25Pushed 6y agoCompare

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

READMEChangelog (1)Dependencies (4)Versions (6)Used By (0)

urodoz/truncateHTML
===================

[](#urodoztruncatehtml)

> PHP library to handle truncate action on HTML strings

Features
--------

[](#features)

- Truncates an HTML string keeping consistency on open/close tags
- No external dependencies.
- PSR-4 compatible.
- Compatible with PHP &gt;= 5.3.3 and
- Integrations for [Symfony2](http://symfony.com) and [Twig](http://twig.sensiolabs.org).

Installation
------------

[](#installation)

You can install urodoz/truncateHTML through [Composer](https://getcomposer.org):

```
$ composer require urodoz/truncate-html:@stable
```

Usage
-----

[](#usage)

Truncate an HTML string:

```
use Urodoz\Truncate\TruncateService;

$truncateService = new TruncateService();
echo $truncateService->truncate($htmlString, 100); //Truncating to 100 characters
```

Integrations
------------

[](#integrations)

### Symfony2

[](#symfony2)

TruncateHTML contains a Symfony2 bundle and service definition that allow you to use it as a service in your Symfony2 application. The code resides in the `Urodoz\Truncate\Bridge\Symfony` namespace and you only need to add the bundle class to your `AppKernel.php`:

```
# app/AppKernel.php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Urodoz\Truncate\Bridge\Symfony\UrodozTruncateBundle(),
        );
        // ...
    }

    // ...
}
```

You can now use the `urodoz_truncate` service everywhere in your application, for example, in your controller:

```
$truncatedString = $this->get('urodoz_truncate')->truncate($htmlString, 100);
```

### Twig

[](#twig)

If you use the Symfony2 framework with Twig you can use the Twig filter `truncateHTML` in your templates after you have setup Symfony2 integrations (see above).

```
{{ truncateHTML(content, 250) }}
```

If you use Twig outside of the Symfony2 framework you first need to add the extension to your environment:

```
use Urodoz\Truncate\Bridge\Twig\TruncateExtension;
use Urodoz\Truncate\TruncateService;

$twig = new Twig_Environment($loader);
$twig->addFunction(new TruncateExtension(TruncateService::create()));
```

You can find more information about registering extensions in the [Twig documentation](http://twig.sensiolabs.org/doc/advanced.html#creating-an-extension).

Author
------

[](#author)

- [Albert Lacarta](https://github.com/urodoz)

License
-------

[](#license)

The MIT License (MIT)

Copyright (c) 2014 Albert Lacarta

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 85.7% 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 ~458 days

Total

5

Last Release

2543d ago

Major Versions

0.0.2 → 1.0.02014-05-25

### Community

Maintainers

![](https://www.gravatar.com/avatar/24fb7c4a91fec2b78834106fecf5f700c7e86d29a1468631d885e3deb368acd8?d=identicon)[bohacpetr](/maintainers/bohacpetr)

---

Top Contributors

[![urodoz](https://avatars.githubusercontent.com/u/1521810?v=4)](https://github.com/urodoz "urodoz (6 commits)")[![bohacpetr](https://avatars.githubusercontent.com/u/10227071?v=4)](https://github.com/bohacpetr "bohacpetr (1 commits)")

---

Tags

htmlcontentshortentruncatetruncating

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[ezyang/htmlpurifier

Standards compliant HTML filter written in PHP

3.3k327.6M445](/packages/ezyang-htmlpurifier)[phpoffice/phpword

PHPWord - A pure PHP library for reading and writing word processing documents (OOXML, ODF, RTF, HTML, PDF)

7.6k34.7M186](/packages/phpoffice-phpword)[urodoz/truncate-html

Handle truncate action on HTML strings

21294.0k6](/packages/urodoz-truncate-html)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[willdurand/negotiation

Content Negotiation tools for PHP provided as a standalone library.

1.4k122.0M157](/packages/willdurand-negotiation)[league/html-to-markdown

An HTML-to-markdown conversion helper for PHP

1.9k28.6M199](/packages/league-html-to-markdown)

PHPackages © 2026

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