PHPackages                             swisnl/textsnippet - 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. [Search &amp; Filtering](/categories/search)
4. /
5. swisnl/textsnippet

ActiveLibrary[Search &amp; Filtering](/categories/search)

swisnl/textsnippet
==================

Create a snippet of text highlighting a given string

3.0.0(6y ago)134.5k1[1 issues](https://github.com/swisnl/textsnippet/issues)[1 PRs](https://github.com/swisnl/textsnippet/pulls)1MITPHPPHP &gt;=7.1CI failing

Since Jul 29Pushed 4y ago5 watchersCompare

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

READMEChangelog (3)Dependencies (4)Versions (4)Used By (1)

SWIS Text Snippet
=================

[](#swis-text-snippet)

Easy and fast way to create a snippet of text, for example for a search result. It will try and highlight the given words and give you the relevant text around it.

[![Build Status](https://camo.githubusercontent.com/baf1f839709ddcb07dda2d68eb1be9eb0ae304716b617ec6c4212a79c7d7ceb2/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f737769736e6c2f74657874736e69707065742f6d61737465722e737667)](https://travis-ci.org/swisnl/textsnippet)[![Latest Stable Version](https://camo.githubusercontent.com/b463f3fb97bf0bb49ddb66ae3d19c0e4f09542f2b951b8bc113973288f933c30/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f737769736e6c2f74657874736e69707065742e737667)](https://packagist.org/packages/swisnl/textsnippet)[![License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](https://packagist.org/packages/swisnl/textsnippet)[![Buy us a tree](https://camo.githubusercontent.com/195a3f79c3c2f91a69498ad26c1d8a7eeaf5771da0007200f409f5d438a515c4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f54726565776172652d2546302539462538432542332d6c69676874677265656e2e737667)](https://plant.treeware.earth/swisnl/textsnippet)

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

[](#installation)

Just use composer to install the package. Or download and include the `TextSnipet.php` file.

`composer require swisnl/textsnippet`

Examples
--------

[](#examples)

Some examples based on a 3 paragraph long Lorum ipsum text.

### Basic usage

[](#basic-usage)

```
$snippet = new TextSnippet()
$snippet->createSnippet('Lorem', $lorumIpsum);
```

Will result in:

**Lorem** ipsum dolor sit amet, consectetur adipiscing elit. ... Etiam bibendum **lorem** nec tempus sollicitudin. ... Sed in dapibus **lorem**. ... Nunc turpis ipsum, bibendum quis sodales sed, ullamcorper et **lorem**. Donec et metus hendrerit, interdum elit ut, dignissim dui.

### Setting highlight html

[](#setting-highlight-html)

You can set the tags surrounding the highlighted text. The `%word%` tag is required.

```
$snippet = new TextSnippet()
$snippet->setHighlightTemplate('%word%')
```

### Setting min and max words

[](#setting-min-and-max-words)

Setting min and maxwords tells the class to try and keep the number of words between the min and max.

```
// Defaults
$minWords = 30;
$maxWords = 100;
```

Setting min and max words.

```
$snippet = new TextSnippet()
$snippet->setMinWords(10);
$snippet->setMaxWords(30);
```

There is a known issue if you set min and max very close to eachother. It might not find the correct set of words/sentences to get exactly between the small gap.

Licence
-------

[](#licence)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

This package is [Treeware](https://treeware.earth). If you use it in production, then we ask that you [**buy the world a tree**](https://plant.treeware.earth/swisnl/textsnippet) to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

SWIS ❤️ Open Source
-------------------

[](#swis-heart-open-source)

[SWIS](https://www.swis.nl) is a web agency from Leiden, the Netherlands. We love working with open source software.

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 62.5% 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 ~499 days

Total

3

Last Release

2209d ago

Major Versions

1.0.0 → 2.0.02017-07-29

2.0.0 → 3.0.02020-04-22

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

3.0.0PHP &gt;=7.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8734305?v=4)[SWIS](/maintainers/swisnl)[@swisnl](https://github.com/swisnl)

---

Top Contributors

[![bbrala](https://avatars.githubusercontent.com/u/3294970?v=4)](https://github.com/bbrala "bbrala (15 commits)")[![janbarasek](https://avatars.githubusercontent.com/u/4738758?v=4)](https://github.com/janbarasek "janbarasek (6 commits)")[![JaZo](https://avatars.githubusercontent.com/u/3475007?v=4)](https://github.com/JaZo "JaZo (2 commits)")[![Vincent-Kleijnendorst](https://avatars.githubusercontent.com/u/653287?v=4)](https://github.com/Vincent-Kleijnendorst "Vincent-Kleijnendorst (1 commits)")

---

Tags

hacktoberfesthighlighterphpsearchsummarizationtext-processing

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/swisnl-textsnippet/health.svg)

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

###  Alternatives

[ruflin/elastica

Elasticsearch Client

2.3k50.4M202](/packages/ruflin-elastica)[opensearch-project/opensearch-php

PHP Client for OpenSearch

15024.3M64](/packages/opensearch-project-opensearch-php)[mailerlite/laravel-elasticsearch

An easy way to use the official PHP ElasticSearch client in your Laravel applications.

934529.3k2](/packages/mailerlite-laravel-elasticsearch)[massive/search-bundle

Massive Search Bundle

721.4M13](/packages/massive-search-bundle)[shyim/opensearch-php-dsl

OpenSearch/Elasticsearch DSL library

175.9M9](/packages/shyim-opensearch-php-dsl)[outl1ne/nova-multiselect-filter

Multiselect filter for Laravel Nova.

45802.7k3](/packages/outl1ne-nova-multiselect-filter)

PHPackages © 2026

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