public static function excerpt($text, $phrase, $radius = 100, $ending = "...") {
		$phrases = is_array($phrase) ? $phrase : array(
			$phrase 
		);
		
		$phraseLen = strlen(implode(' ', $phrases));
		if($radius < $phraseLen) {
			$radius = $phraseLen;
		}
		
		foreach($phrases as $phrase) {
			$pos = strpos(strtolower($text), strtolower($phrase));
			if($pos > -1)
				break;
		}
		
		$startPos = 0;
		if($pos > $radius) {
			$startPos = $pos - $radius;
		}
		
		$textLen = strlen($text);
		
		$endPos = $pos + $phraseLen + $radius;
		if($endPos >= $textLen) {
			$endPos = $textLen;
		}
		
		$excerpt = substr($text, $startPos, $endPos - $startPos);
		if($startPos != 0) {
			$excerpt = substr_replace($excerpt, $ending, 0, $phraseLen);
		}
		
		if($endPos != $textLen) {
			$excerpt = substr_replace($excerpt, $ending, -$phraseLen);
		}
		
		return $excerpt;
	}
public static function excerpt($text, $phrase, $radius = 100, $ending = "...") {
		$phrases = is_array($phrase) ? $phrase : array(
			$phrase 
		);
		
		$phraseLen = strlen(implode(' ', $phrases));
		if($radius < $phraseLen) {
			$radius = $phraseLen;
		}
		
		foreach($phrases as $phrase) {
			$pos = strpos(strtolower($text), strtolower($phrase));
			if($pos > -1)
				break;
		}
		
		$startPos = 0;
		if($pos > $radius) {
			$startPos = $pos - $radius;
		}
		
		$textLen = strlen($text);
		
		$endPos = $pos + $phraseLen + $radius;
		if($endPos >= $textLen) {
			$endPos = $textLen;
		}
		
		$excerpt = substr($text, $startPos, $endPos - $startPos);
		if($startPos != 0) {
			$excerpt = substr_replace($excerpt, $ending, 0, $phraseLen);
		}
		
		if($endPos != $textLen) {
			$excerpt = substr_replace($excerpt, $ending, -$phraseLen);
		}
		
		return $excerpt;
	}
public static function excerpt ( $ text , $ phrase , $ radius = 100 , $ ending = "..." ) { $ phrases = is_array ( $ phrase ) ? $ phrase : array ( $ phrase ) ; $ phraseLen = strlen ( implode ( ' ' , $ phrases ) ) ; if ( $ radius < $ phraseLen ) { $ radius = $ phraseLen ; } foreach ( $ phrases as $ phrase ) { $ pos = strpos ( strtolower ( $ text ) , strtolower ( $ phrase ) ) ; if ( $ pos > - 1 ) break ; } $ startPos = 0 ; if ( $ pos > $ radius ) { $ startPos = $ pos - $ radius ; } $ textLen = strlen ( $ text ) ; $ endPos = $ pos + $ phraseLen + $ radius ; if ( $ endPos >= $ textLen ) { $ endPos = $ textLen ; } $ excerpt = substr ( $ text , $ startPos , $ endPos - $ startPos ) ; if ( $ startPos != 0 ) { $ excerpt = substr_replace ( $ excerpt , $ ending , 0 , $ phraseLen ) ; } if ( $ endPos != $ textLen ) { $ excerpt = substr_replace ( $ excerpt , $ ending , - $ phraseLen ) ; } return $ excerpt ; }
Creates an excerpt from the given text based on the passed phrase

@see http://stackoverflow.com/a/1404151

@param string $text
The text to extract the excerpt from
@param string|array $phrase
The phrases to search for.
@param number $radius
The radius in characters to be included in the excerpt
@param string $ending
The string that should be appended after the excerpt
@return string The created excerpt
Creates an excerpt from the given text based on the passed phrase