API16

JHtmlString/truncate: Difference between revisions

From Joomla! Documentation

Dextercowley (talk | contribs)
fix incorrect importing section
m removing red link to edit, no existant pages
Line 3: Line 3:


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


{{Description:JHtmlString/truncate}}
<! removed transcluded page call, red link never existed >


===Syntax===
===Syntax===
Line 62: Line 62:


<span class="editsection" style="font-size:76%;">
<span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>[[SeeAlso:JHtmlString/truncate|Edit See Also]]<nowiki>]</nowiki>
<nowiki>[<! removed edit link to red link >]</nowiki>
</span>
</span>
{{SeeAlso:JHtmlString/truncate}}
<! removed transcluded page call, red link never existed >


===Examples===
===Examples===
Line 81: Line 81:
  format= ,,,
  format= ,,,
</dpl>
</dpl>
[[Category:Archived pages API16]]

Revision as of 02:58, 14 May 2013

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 edit link to red link >]

<! 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 edit link to red link >] <! removed transcluded page call, red link never existed >

Examples

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

<CodeExamplesForm />