xbTruncString Plugin
Intro Page

The xbTruncString plugin is a system plugin to override the default JHtml::_('string.truncate') function.

public static function truncate($text, $length = 0, $noSplit = true, $allowHtml = true)

Usage: In Joomla

$result = JHtml::_('string.truncate', $text, $len, $noSplit, $allowHtml)

$text is the string to be truncated which can include html tags. $len is the desired maximm length excluding ellipsis. $noSplit is a boolean value, if true the the string will be truncated at the word break before the desired length. If $allowHtml is true then the result will include any Html tags in the desired length and an attempt will be made to close and tags that would be let open by the truncate

 

For JHtml helper (and other helper) functions, as long as they used as above and not called directly (eg by using JHtmlString::truncate() ) it is possible to register a custom handler for the function which will override the default in the library.

Helper functions should always be called using the format JHtml::_('string.truncate',...). So long as this format is used then the xbTruncString plugin will override the function with its own bug-fixed version.

It does this by registering a custom handler for the target function name ('string.truncate') using a listener in its onAfterInitialise() function.

I am indebted to Michael Babker for showing me how to do this in his code example at https://github.com/mbabker/bs3-demo/blob/master/plg_system_bootstrap3/bootstrap3.php which I have adapted to wrap the bug-fixed truncate() function.

Install the plugin in the usual way and don't forget to enable it. It works with Joomla 3.9 and probably most of the 3.x series. (the truncate bugs have existed since at least J2.5). It uses the update system, although I hope there won't be any updates!