Back to StringHelper class

Method abridge

public static string
abridge
(mixed $text, mixed $length = 50, mixed $intro = 30)
Abridges text strings over the specified character limit. The behavior will insert an ellipsis into the text replacing a section of variable size to ensure the string does not exceed the defined maximum length. This method is UTF-8 safe.
Parameters
  • string $text The text to abridge.
  • int $length The maximum length of the text (default is 50).
  • int $intro The maximum length of the intro text (default is 30).
Returns
  • string The abridged text.
Since
  • 1.6
Class: StringHelper
Project: Joomla

Method abridge - Source code

/**
 * Abridges text strings over the specified character limit. The
 * behavior will insert an ellipsis into the text replacing a section
 * of variable size to ensure the string does not exceed the defined
 * maximum length. This method is UTF-8 safe.
 *
 * For example, it transforms "Really long title" to "Really...title".
 *
 * Note that this method does not scan for HTML tags so will potentially break them.
 *
 * @param   string   $text    The text to abridge.
 * @param   integer  $length  The maximum length of the text (default is 50).
 * @param   integer  $intro   The maximum length of the intro text (default is 30).
 *
 * @return  string   The abridged text.
 *
 * @since   1.6
 */
public static function abridge($text, $length = 50, $intro = 30)
{
    // Abridge the item text if it is too long.
    if (FrameworkStringHelper::strlen($text) > $length) {
        // Determine the remaining text length.
        $remainder = $length - ($intro + 3);
        // Extract the beginning and ending text sections.
        $beg = FrameworkStringHelper::substr($text, 0, $intro);
        $end = FrameworkStringHelper::substr($text, FrameworkStringHelper::strlen($text) - $remainder);
        // Build the resulting string.
        $text = $beg . '...' . $end;
    }
    return $text;
}