english/libraries/joomla/html/html/string.php
/**
	 * Truncates text blocks over the specified character limit and closes
	 * all open HTML tags. 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.
	 *
	 * @param   string   $text		The text to truncate.
	 * @param   integer  $length		The maximum length of the text.
	 * @return  string   The truncated text.
	 */
Truncates text blocks over the specified character limit and closes
all open HTML tags. 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.

@param   string   $text              The text to truncate.
@param   integer  $length            The maximum length of the text.
@return  string   The truncated text.
Truncates text blocks over the specified character limit and closes all open HTML tags . 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 . @param string $text The text to truncate . @param integer $length The maximum length of the text . @return string The truncated text .
public static function truncate($text, $length = 0)
	{
		if ($length > 0 && JString::strlen($text) > $length)
		{
			$tmp = JString::substr($text, 0, $length);
			$offset = JString::strrpos($tmp, ' ');
			if(JString::strrpos($tmp, '<') > JString::strrpos($tmp, '>'))
			{
				$offset = JString::strrpos($tmp, '<');
			}
			$tmp = JString::substr($tmp, 0, $offset);
			if (JString::strlen($tmp) >= $length - 3) {
				$tmp = JString::substr($tmp, 0, JString::strrpos($tmp, ' '));
			}
			preg_match_all ( "#<([a-z][a-z0-9]?)( .*)?(?!/)>#iU", $tmp, $result );
			$openedtags = $result[1];
			$openedtags = array_diff($openedtags, array("img", "hr", "br"));
			$openedtags = array_values($openedtags);
			preg_match_all ( "#</([a-z]+)>#iU", $tmp, $result );
			$closedtags = $result[1];
			$len_opened = count ( $openedtags );
			if( count ( $closedtags ) == $len_opened )
			{
				return $tmp.'...';
			}
			$openedtags = array_reverse ( $openedtags );
			for( $i = 0; $i < $len_opened; $i++ )
			{
				if ( !in_array ( $openedtags[$i], $closedtags ) )
				{
					$tmp .= "</" . $openedtags[$i] . ">";
				} else {
					unset ( $closedtags[array_search ( $openedtags[$i], $closedtags)] );
				}
			}
			$text = $tmp.'...';
		}
		return $text;
	}
public static function truncate ( $ text , $ length = 0 ) { if ( $ length > 0 && JString :: strlen ( $ text ) > $ length ) { $ tmp = JString :: substr ( $ text , 0 , $ length ) ; $ offset = JString :: strrpos ( $ tmp , ' ' ) ; if ( JString :: strrpos ( $ tmp , '<' ) > JString :: strrpos ( $ tmp , '>' ) ) { $ offset = JString :: strrpos ( $ tmp , '<' ) ; } $ tmp = JString :: substr ( $ tmp , 0 , $ offset ) ; if ( JString :: strlen ( $ tmp ) >= $ length - 3 ) { $ tmp = JString :: substr ( $ tmp , 0 , JString :: strrpos ( $ tmp , ' ' ) ) ; } preg_match_all ( " #<([a-z][a-z0-9]?)( .*)?(?!/)>#iU " , $ tmp , $ result ) ; $ openedtags = $ result [ 1 ] ; $ openedtags = array_diff ( $ openedtags , array ( " img " , " hr " , " br " ) ) ; $ openedtags = array_values ( $ openedtags ) ; preg_match_all ( " #</([a-z]+)>#iU " , $ tmp , $ result ) ; $ closedtags = $ result [ 1 ] ; $ len_opened = count ( $ openedtags ) ; if ( count ( $ closedtags ) == $ len_opened ) { return $ tmp . '...' ; } $ openedtags = array_reverse ( $ openedtags ) ; for ( $ i = 0 ; $ i < $ len_opened ; $ i ++ ) { if ( ! in_array ( $ openedtags [ $ i ] , $ closedtags ) ) { $ tmp .= " </ " . $ openedtags [ $ i ] . " > " ; } else { unset ( $ closedtags [ array_search ( $ openedtags [ $ i ] , $ closedtags ) ] ) ; } } $ text = $ tmp . '...' ; } return $ text ; }
Truncates text blocks over the specified character limit and closes
 all open HTML tags.
Truncates text blocks over the specified character limit and closes all open HTML tags .
// Truncate the item text if it is too long. // Find the first space within the allowed length. // If we don't have 3 characters of room, go to the second space within the limit. // Put all opened tags into an array // Put all closed tags into an array // All tags are closed // Close tags