Trim a sentence with length, find last word and add ellipse

Posted on: December 14, 2010 Posted by: John Huh Comments: 0

Trim a sentence with length, find last word and add ellipse

[php]
$len = 40;

$str = ‘Returns the numeric position of the first occurrence of needle in the haystack string. Unlike the strrpos() before PHP 5, this function can take a full string as the needle parameter and the entire string will be used.’;

echo trunc_description($str, $len, TRUE);

/**
* Truncate the Description to something shorter
* @param string $text
* @param integer $length
* @param bool $add_ellipse
* @return string
*/
function trunc_description( $text, $limit = 25, $add_ellipse = true )
{
oif( strlen( $text ) > $limit )
o{
o$text = substr($text, 0, strpos($text, ‘ ‘, $limit) );
oif( $add_ellipse === true )
o{
o$text .= ‘…’;
o}
o}
oreturn $text;
}

[/php]

Leave a Reply:

Your email address will not be published. Required fields are marked *