Getting Excerpt From A Long Paragraph

This function can be used to get excerpt from long paragraph according to design needs.

/**
* Get excerpt from string
*
* @param String $str String to get an excerpt from
* @param Integer $startPos Position int string to start excerpt from
* @param Integer $maxLength Maximum length the excerpt may be
* @return String excerpt
*/
function getExcerpt($str, $startPos=0, $maxLength=100) {
if(strlen($str) > $maxLength) {
$excerpt = substr($str, $startPos, $maxLength-3);
$lastSpace = strrpos($excerpt, ‘ ‘);
$excerpt = substr($excerpt, 0, $lastSpace);
$excerpt .= ‘…’;
} else {
$excerpt = $str;
}

return $excerpt;
}