Custom Zend Filter: Excerpt

So you are familiar with the WordPress template tag called the_excert() right? If not the tag takes a post and trims it up to display 55 or so words and then adds ellipses to the end. I find this very useful when you just want to display a teaser. In the current Zend Framework project then I am working on for a client I needed just this. So I had to create the custom filter which is really easy. You can find a vast amount of info for Zend Filters here.

Custom Filter – Excerpt

// To call the filter use
$excerpt = new My_Filter_Excerpt();
$excerpt->filter($str); //$str is the string of text

//Put this in your library
class My_Filter_Excerpt implements Zend_Filter_Interface
{
	protected $_wordCount = 55;

	public function filter($data)
	{
		$sentance = explode(" ", $data);
		if (count($sentance) > $this->_wordCount) {
			$sentance = array_splice($sentance, 0, $this->_wordCount);
			$ellipsis = "…";
		}
		$excerpt = implode(" ", $sentance);
		echo $excerpt . $ellipsis;
	}
}