Download the PHP package sashabo/shortener without Composer
On this page you can find all versions of the php package sashabo/shortener. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sashabo/shortener
More information about sashabo/shortener
Files in sashabo/shortener
Package shortener
Short Description Shortens plain text or html to the $length, preventing braking words and tags.
License GPL-3.0-only
Informations about the package shortener
sasha-bo/shortener
Shortens plain text or html to the $length, preventing braking words and tags.
Shortening plain text
To cut plain text string to the $length use this static method:
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:
So the source string will be parsed only once.
Shortening HTML
To cut HTML string to the $length use this static method:
or:
Results:
As you see, no closing tags are lost. If your source string is valid HTML, you will get valid HTML too.
HtmlShortener understands &xxx; symbols and count them as 1.
Using with Twig
The package contains class providing and twig filters.
To use them, just register the extension. For example, in Symfony add this to your config/services.yaml: