{shorten:text:length:mode:add}

Description

Shortens a text to at most length characters and always strips HTML tags first. If the text already fits within length, it is returned unchanged (HTML kept); otherwise the HTML is removed and the result is cut to length. The mode parameter chooses where to cut: 0 cuts at the exact length, 1 tries to stop at the end of a paragraph or sentence, 2 (the default) is smart - it behaves like 0 for length under 50 and like 1 for length 50 or more, and 3 keeps the start and the end of the text and joins them with an ellipsis in the middle. The add text is appended only when the text was actually shortened, so the result can be up to length plus add long. Parameters are not trimmed, so a leading space in add (for example " ...") is preserved.

Parameters

text required

The text to shorten. HTML tags are stripped before the length is measured (unless the original text already fits within length, in which case it is returned untouched).

length optional default 49

Maximum number of characters to keep. The value is cast to an integer.

mode optional default 2

Where to cut. 0 cuts at the exact length; 1 tries to stop at the end of a paragraph or sentence; 2 (default) is smart - it uses mode 0 for length under 50 and mode 1 for 50 or more; 3 keeps the start and end and joins them with an ellipsis in the middle.

add optional

Text appended only when the input was actually shortened (so the result can be up to length plus add long). Parameters are not trimmed, so a leading space such as in the value space-dot-dot-dot is preserved.

Examples

test{shorten:The quick brown fox jumps over the lazy dog.:25:0: ...}
ExpectedThe quick brown fox jumps ...
ActualThe quick brown fox jumps ...
The fourth parameter is appended only when the text was actually shortened. Parameters are not trimmed, so the leading space in " ..." is kept.
test{shorten:Hello world this is a long text:10}
ExpectedHello worl
ActualHello worl
With only the text and a length, shorten cuts to that many characters. Mode defaults to 2 (smart), which acts like mode 0 for lengths under 50.
test{shorten:First sentence here. And much more text beyond the limit.:30:1}
ExpectedFirst sentence here.
ActualFirst sentence here.
Mode 1 tries to cut at the end of a paragraph or sentence rather than mid-word. Here it stops right after the first sentence.
virtual{shorten:The quick brown fox jumps over the lazy dog.:20:3}
Expected(start and end joined by an ellipsis)
ActualThe quick… lazy dog.
Mode 3 keeps the start and the end of the text and joins them with an ellipsis in the middle, instead of cutting only from the end.
test{shorten:Short note:49}
ExpectedShort note
ActualShort note
When the text is already within length, shorten returns it unchanged and does not append add. Short note is 10 characters, under the limit of 49.
test{shorten:<b>Bold title</b> with more words:10}
ExpectedBold title
ActualBold title
HTML tags are removed before the length is measured and cut. The bold tags drop out, leaving plain text trimmed to 10 characters.