PHPackages                             marcgoertz/shorten - 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. marcgoertz/shorten

ActiveLibrary

marcgoertz/shorten
==================

Safely truncate HTML markup while preserving tags, handling entities, and supporting Unicode/emoji with optional word-safe truncation

5.0.1(9mo ago)129.3k2[1 PRs](https://github.com/mrcgrtz/php-shorten/pulls)1MITPHPPHP &gt;=8.2CI passing

Since May 2Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/mrcgrtz/php-shorten)[ Packagist](https://packagist.org/packages/marcgoertz/shorten)[ Docs](https://github.com/mrcgrtz/php-shorten)[ Fund](https://ko-fi.com/mrcgrtz)[ Fund](https://liberapy.com/mrcgrtz)[ RSS](/packages/marcgoertz-shorten/feed)WikiDiscussions main Synced 1mo ago

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

Shorten
=======

[](#shorten)

> Safely truncate HTML markup while preserving tags, handling entities, and supporting Unicode/emoji with optional word-safe truncation.

[![Test](https://github.com/mrcgrtz/php-shorten/actions/workflows/php.yml/badge.svg)](https://github.com/mrcgrtz/php-shorten/actions/workflows/php.yml)[![Coverage Status](https://camo.githubusercontent.com/19ee81bbf3257f16ad08cbbadf4dc3bcbc70ea70cd1679ee67a8b2f13f246b88/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6d72636772747a2f7068702d73686f7274656e2f62616467652e7376673f6272616e63683d6d61696e)](https://coveralls.io/github/mrcgrtz/php-shorten?branch=main)[![Packagist PHP Version Support](https://camo.githubusercontent.com/18c7ab87472b970e5d6fc035624efa2b6336128da05e225db97f65e2439231f2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6d617263676f6572747a2f73686f7274656e)](https://camo.githubusercontent.com/18c7ab87472b970e5d6fc035624efa2b6336128da05e225db97f65e2439231f2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6d617263676f6572747a2f73686f7274656e)[![Packagist Downloads](https://camo.githubusercontent.com/8bbd045c8909eaf1b1f432a6f4e537c3041029c35936176ef6ca2b2a8a09e371/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d617263676f6572747a2f73686f7274656e)](https://camo.githubusercontent.com/8bbd045c8909eaf1b1f432a6f4e537c3041029c35936176ef6ca2b2a8a09e371/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d617263676f6572747a2f73686f7274656e)[![Packagist Stars](https://camo.githubusercontent.com/f870d7b6920b6d1e515fd9ac54c34002d460b985f4b25b9fb0de93233e1779d0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f73746172732f6d617263676f6572747a2f73686f7274656e)](https://camo.githubusercontent.com/f870d7b6920b6d1e515fd9ac54c34002d460b985f4b25b9fb0de93233e1779d0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f73746172732f6d617263676f6572747a2f73686f7274656e)[![MIT License](https://camo.githubusercontent.com/a842f0dccdcaaa40ef6c0ff1ed5620743246c92dde02749d2b977791a2814ded/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6d72636772747a2f7068702d73686f7274656e)](https://github.com/mrcgrtz/php-shorten/blob/main/LICENSE.md)

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

[](#installation)

I recommend using [Composer](https://getcomposer.org/) for installing and using Shorten:

```
composer require marcgoertz/shorten
```

Of course you can also just require it in your scripts directly.

Usage
-----

[](#usage)

```

```

Output:

```
Go to exam…
```

Functions
---------

[](#functions)

### `truncateMarkup()`

[](#truncatemarkup)

```
truncateMarkup(
    string $markup,
    int $length = 400,
    string $appendix = '…',
    bool $appendixInside = false,
    bool $wordsafe = false,
    string $delimiter = ' '
): string
```

#### Parameters

[](#parameters)

- `string $markup`: Text containing markup
- `int $length`: Maximum length of truncated text (default: `400`)
- `string $appendix`: Text added after truncated text (default: `'…'`)
- `bool $appendixInside`: Add appendix to last content in tags, increases `$length` by 1 (default: `false`)
- `bool $wordsafe`: Wordsafe truncation, cuts at word boundaries (default: `false`)
- `string $delimiter`: Delimiter for wordsafe truncation (default: `' '`)

#### Examples

[](#examples)

```
