https://github.com/swisnl/textsnippet
Create a snippet of text highlighting a given string
https://github.com/swisnl/textsnippet
hacktoberfest highlighter php search summarization text-processing
Last synced: 4 months ago
JSON representation
Create a snippet of text highlighting a given string
- Host: GitHub
- URL: https://github.com/swisnl/textsnippet
- Owner: swisnl
- License: mit
- Created: 2017-07-28T13:33:31.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2021-10-07T12:43:16.000Z (about 4 years ago)
- Last Synced: 2025-06-06T05:30:35.758Z (4 months ago)
- Topics: hacktoberfest, highlighter, php, search, summarization, text-processing
- Language: PHP
- Homepage:
- Size: 18.6 KB
- Stars: 14
- Watchers: 6
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# 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.
[](https://travis-ci.org/swisnl/textsnippet)
[](https://packagist.org/packages/swisnl/textsnippet)
[](https://packagist.org/packages/swisnl/textsnippet)
[](https://plant.treeware.earth/swisnl/textsnippet)## Installation
Just use composer to install the package. Or download and include the `TextSnipet.php` file.
``composer require swisnl/textsnippet``
## Examples
Some examples based on a 3 paragraph long Lorum ipsum text.
### Basic usage
```php
$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
You can set the tags surrounding the highlighted text. The `%word%` tag is required.
```php
$snippet = new TextSnippet()
$snippet->setHighlightTemplate('%word%')
```### 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.
```php
// Defaults
$minWords = 30;
$maxWords = 100;
```Setting min and max words.
```php
$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
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 :heart: Open Source
[SWIS](https://www.swis.nl) is a web agency from Leiden, the Netherlands. We love working with open source software.