Get excerpt from string

Get excerpt from string

<?php

/**
 * 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;
}

?>

Usage

$excerpt = getExcerpt($longText, 0, 150);


Comments

guest
Posted on 04.03.2014 13:27

Thank\'s! Very simple!

guest
Posted on 04.11.2013 09:00

very good... simple for lazy people :D ...

guest
Posted on 08.11.2012 10:59

Thanks for the function :)

guest
Posted on 15.01.2012 16:20

Thank you

Add your comment