PHPackages                             sashabo/shortener - 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. sashabo/shortener

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

sashabo/shortener
=================

Shortens plain text or html to the $length, preventing braking words and tags.

1.2(2y ago)26.7k↓50%GPL-3.0-onlyPHPPHP &gt;=8.1

Since Oct 22Pushed 2y ago1 watchersCompare

[ Source](https://github.com/sasha-bo/shortener)[ Packagist](https://packagist.org/packages/sashabo/shortener)[ RSS](/packages/sashabo-shortener/feed)WikiDiscussions main Synced 1mo ago

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

sasha-bo/shortener
==================

[](#sasha-boshortener)

Shortens plain text or html to the $length, preventing braking words and tags.

Shortening plain text
---------------------

[](#shortening-plain-text)

To cut plain text string to the $length use this static method:

```
Shortener::shortenText(string $source, int $length = 50, string $add = '...', bool $multiSpace = false): string
```

The shortener will never break a word, excepting the case if the first word is longer than $length. The shortener uses mb\_strlen, not strlen, so it counts UTF-8 symbols correctly. If $multiSpace is true, the shortener counts each space (including \\n, etc) as one symbol, otherwise a group of spaces is counted as 1 symbol.

For example, if the source is "Lorem ipsum dolor sit amet", here are lengths and results:

26: Lorem ipsum dolor sit amet (full string)

25: Lorem ipsum dolor sit...

10: Lorem...

6: Lor...

Those ... are counted too. So, the result string will have the required length including the length of $add

If you need to shorten one string few times, use this way for better performance:

```
$shortener = new TextShortener('Lorem ipsum dolor');
echo $shortener->shorten(10, '...');
```

So the source string will be parsed only once.

Shortening HTML
---------------

[](#shortening-html)

To cut HTML string to the $length use this static method:

```
Shortener::shortenHtml(string $source, int $length = 50, string $add = '...'): string
```

or:

```
$shortener = new HtmlShortener('Lorem ipsum dolor sit amet');
echo $shortener->shorten(26, '...');
echo $shortener->shorten(25, '...');
echo $shortener->shorten(10, '...');
echo $shortener->shorten(6, '...');
```

Results:

```
Lorem ipsum dolor sit amet (full string)
Lorem ipsum dolor sit...
Lorem...
Lor...
```

As you see, no closing tags are lost. If your source string is valid HTML, you will get valid HTML too.

HtmlShortener understands &amp;xxx; symbols and count them as 1.

### Using with Twig

[](#using-with-twig)

The package contains `ShortenerTwigExtension` class providing `shorten_text` and `shorten_html` twig filters.

```
{{ 'Lorem ipsum dolor sit amet' | shorten_text(25, '...') }}

{{ 'Lorem ipsum dolor sit amet' | shorten_html(25, '...') }}
```

To use them, just register the extension. For example, in Symfony add this to your config/services.yaml:

```
services:
    shortener_twig_extension:
        class: SashaBo\Shortener\Extras\ShortenerTwigExtension
        tags:
            - { name: twig.extension }
```

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 90.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 ~135 days

Total

2

Last Release

805d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5369c71152f0feb0c57d12044c87c7f8be9c4640586b042629a6c50638d4e2e8?d=identicon)[sashabo](/maintainers/sashabo)

---

Top Contributors

[![xbndrv](https://avatars.githubusercontent.com/u/151878218?v=4)](https://github.com/xbndrv "xbndrv (10 commits)")[![sasha-bo](https://avatars.githubusercontent.com/u/146867796?v=4)](https://github.com/sasha-bo "sasha-bo (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/sashabo-shortener/health.svg)

```
[![Health](https://phpackages.com/badges/sashabo-shortener/health.svg)](https://phpackages.com/packages/sashabo-shortener)
```

###  Alternatives

[maba/webpack-bundle

Bundle to Integrate Webpack to Symfony

123268.2k4](/packages/maba-webpack-bundle)[bueltge/must-use-loader

Load Must-Use Plugins inside subdirectories with caching. For delete the cache: if you view the Must Use plugin list in the network administration.

6115.7k2](/packages/bueltge-must-use-loader)

PHPackages © 2026

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