JOOMLA FICTION LAB
(4 votes)

How to truncate text over a specified character limit in Joomla

Tuesday, 24 April 2012 13:06
When you want to display only a certain amount of characters of a text block in Joomla, you can use the class JHTML and the method truncate.
This method will not cut words in half but instead it will truncate at the first space in the defined limit.

$truncated_text = JHtml::_('string.truncate', $text, 100);
echo $truncated_text;
where $text = the original text block and
100 = the characters limit

Example


$text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
$truncated_text = JHtml::_('string.truncate', $text, 100);
echo $truncated_text; // outputs 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut...'




Add comment


Security code
Refresh