API16

JHtmlString/truncate: Difference between revisions

From Joomla! Documentation

m removing red link to edit, no existant pages
m preparing for archive only
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:
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.
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.


<span class="editsection" style="font-size:76%;">
 
<nowiki>[<! removed edit link to red link >]</nowiki>
</span>


<! removed transcluded page call, red link never existed >
<! removed transcluded page call, red link never existed >
Line 61: Line 59:
</source>
</source>


<span class="editsection" style="font-size:76%;">
 
<nowiki>[<! removed edit link to red link >]</nowiki>
</span>
<! removed transcluded page call, red link never existed >
<! removed transcluded page call, red link never existed >


Line 70: Line 66:
echo JHTML::_('string.truncate', ($this->item->title), $params->get('readmore_limit'));
echo JHTML::_('string.truncate', ($this->item->title), $params->get('readmore_limit'));
</source>
</source>
<CodeExamplesForm />
=== Code Examples ===


<dpl>
<dpl>
Line 76: Line 72:
  category=truncate
  category=truncate
  category=JHtmlString
  category=JHtmlString
  category=CodeExample
  namespace=CodeExample
  category=MethodExample
  category=MethodExample
  include=*
  include=*

Latest revision as of 01:47, 25 March 2017

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.


<! removed transcluded page call, red link never existed >

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;
}


<! removed transcluded page call, red link never existed >

Examples

echo JHTML::_('string.truncate', ($this->item->title), $params->get('readmore_limit'));

Code Examples