API16:JHtmlString/truncate
From Joomla! Documentation
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.
Contents |
Description
Truncates text blocks over the specified character limit. The behavior will not truncate an individual word, it will find the first space that is within the limit and truncate at that point. This method is UTF-8 safe.
Description:JHtmlString/truncate
Syntax
truncate($text, $length=0)
| Parameter Name | Default Value | Description |
|---|---|---|
| $text | $text The text to truncate. | |
| $length | 0 | $length The maximum length of the text. |
Returns
string The truncated text.
Defined in
libraries/joomla/html/html/string.php
Importing
JLoader::register('JHtmlString', JPATH_LIBRARIES.'/joomla/html/html/string.php');
Source Body
function truncate($text, $length = 0) { // Truncate the item text if it is too long. if ($length > 0 && JString::strlen($text) > $length) { // Find the first space within the allowed length. $tmp = JString::substr($text, 0, $length); $tmp = JString::substr($tmp, 0, JString::strrpos($tmp, ' ')); // If we don't have 3 characters of room, go to the second space within the limit. if (JString::strlen($tmp) >= $length - 3) { $tmp = JString::substr($tmp, 0, JString::strrpos($tmp, ' ')); } $text = $tmp.'...'; } return $text; }
[Edit See Also] SeeAlso:JHtmlString/truncate
Examples
echo JHTML::_('string.truncate', ($this->item->title), $params->get('readmore_limit'));
<CodeExamplesForm />
