english/modules/mod_articles_category/helper.php
/**
	* This is a better truncate implementation than what we
	* currently have available in the library. In particular,
	* on index.php/Banners/Banners/site-map.html JHtml's truncate
	* method would only return "Article...". This implementation
	* was taken directly from the Stack Overflow thread referenced
	* below. It was then modified to return a string rather than
	* print out the output and made to use the relevant JString
	* methods.
	*
	* @link http://stackoverflow.com/questions/1193500/php-truncate-html-ignoring-tags
	* @param mixed $html
	* @param mixed $maxLength
	*/
This is a better truncate implementation than what we
currently have available in the library. In particular,
on index.php/Banners/Banners/site-map.html JHtml's truncate
method would only return "Article...". This implementation
was taken directly from the Stack Overflow thread referenced
below. It was then modified to return a string rather than
print out the output and made to use the relevant JString
methods.


This is a better truncate implementation than what we currently have available in the library . In particular on index . php / Banners / Banners / site - map . html JHtml ' s truncate method would only return " Article ... " . This implementation was taken directly from the Stack Overflow thread referenced below . It was then modified to return a string rather than print out the output and made to use the relevant JString methods .
public static function truncate($html, $maxLength = 0)
	{
		$printedLength = 0;
		$position = 0;
		$tags = array();
		$output = '';
		if (empty($html)) {
			return $output;
		}
		while ($printedLength < $maxLength && preg_match('{</?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;}', $html, $match, PREG_OFFSET_CAPTURE, $position))
		{
			list($tag, $tagPosition) = $match[0];
			$str = JString::substr($html, $position, $tagPosition - $position);
			if ($printedLength + JString::strlen($str) > $maxLength) {
				$output .= JString::substr($str, 0, $maxLength - $printedLength);
				$printedLength = $maxLength;
				break;
			}
			$output .= $str;
			$lastCharacterIsOpenBracket = (JString::substr($output, -1, 1) === '<');
			if ($lastCharacterIsOpenBracket) {
				$output = JString::substr($output, 0, JString::strlen($output) - 1);
			}
			$printedLength += JString::strlen($str);
			if ($tag[0] == '&') {
				$output .= $tag;
				$printedLength++;
			}
			else {
				$tagName = $match[1][0];
				if ($tag[1] == '/') {
					$openingTag = array_pop($tags);
					$output .= $tag;
				}
				else if ($tag[JString::strlen($tag) - 2] == '/') {
					$output .= $tag;
				}
				else {
					$output .= $tag;
					$tags[] = $tagName;
				}
			}
			if ($lastCharacterIsOpenBracket) {
				$position = ($tagPosition - 1) + JString::strlen($tag);
			}
			else {
				$position = $tagPosition + JString::strlen($tag);
			}
		}
		if ($printedLength < $maxLength && $position < JString::strlen($html)) {
			$output .= JString::substr($html, $position, $maxLength - $printedLength);
		}
		while (!empty($tags))
		{
			$output .= sprintf('</%s>', array_pop($tags));
		}
		$length = JString::strlen($output);
		$lastChar = JString::substr($output, ($length - 1), 1);
		$characterNumber = ord($lastChar);
		if ($characterNumber === 194) {
			$output = JString::substr($output, 0, JString::strlen($output) - 1);
		}
		$output = JString::rtrim($output);
		return $output.'&hellip;';
	}
public static function truncate ( $ html , $ maxLength = 0 ) { $ printedLength = 0 ; $ position = 0 ; $ tags = array ( ) ; $ output = '' ; if ( empty ( $ html ) ) { return $ output ; } while ( $ printedLength < $ maxLength && preg_match ( '{</?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;}' , $ html , $ match , PREG_OFFSET_CAPTURE , $ position ) ) { list ( $ tag , $ tagPosition ) = $ match [ 0 ] ; $ str = JString :: substr ( $ html , $ position , $ tagPosition - $ position ) ; if ( $ printedLength + JString :: strlen ( $ str ) > $ maxLength ) { $ output .= JString :: substr ( $ str , 0 , $ maxLength - $ printedLength ) ; $ printedLength = $ maxLength ; break ; } $ output .= $ str ; $ lastCharacterIsOpenBracket = ( JString :: substr ( $ output , - 1 , 1 ) === '<' ) ; if ( $ lastCharacterIsOpenBracket ) { $ output = JString :: substr ( $ output , 0 , JString :: strlen ( $ output ) - 1 ) ; } $ printedLength += JString :: strlen ( $ str ) ; if ( $ tag [ 0 ] == '&' ) { $ output .= $ tag ; $ printedLength ++ ; } else { $ tagName = $ match [ 1 ] [ 0 ] ; if ( $ tag [ 1 ] == '/' ) { $ openingTag = array_pop ( $ tags ) ; $ output .= $ tag ; } else if ( $ tag [ JString :: strlen ( $ tag ) - 2 ] == '/' ) { $ output .= $ tag ; } else { $ output .= $ tag ; $ tags [ ] = $ tagName ; } } if ( $ lastCharacterIsOpenBracket ) { $ position = ( $ tagPosition - 1 ) + JString :: strlen ( $ tag ) ; } else { $ position = $ tagPosition + JString :: strlen ( $ tag ) ; } } if ( $ printedLength < $ maxLength && $ position < JString :: strlen ( $ html ) ) { $ output .= JString :: substr ( $ html , $ position , $ maxLength - $ printedLength ) ; } while ( ! empty ( $ tags ) ) { $ output .= sprintf ( '</%s>' , array_pop ( $ tags ) ) ; } $ length = JString :: strlen ( $ output ) ; $ lastChar = JString :: substr ( $ output , ( $ length - 1 ) , 1 ) ; $ characterNumber = ord ( $ lastChar ) ; if ( $ characterNumber === 194 ) { $ output = JString :: substr ( $ output , 0 , JString :: strlen ( $ output ) - 1 ) ; } $ output = JString :: rtrim ( $ output ) ; return $ output . '&hellip;' ; }
This is a better truncate implementation than what we
 currently have available in the library.
This is a better truncate implementation than what we currently have available in the library .
// Print text leading up to the tag. // Handle the entity. // Handle the tag. // This is a closing tag. // Self-closing tag. // Opening tag. // Continue after the tag. // Print any remaining text. // Close any open tags.