Revision: 44706
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at April 16, 2011 01:32 by prwhitehead
Initial Code
/** * usage: * $txt = new($post->post_content, 50, 'Read in full', '<span class="readmore">', '</span>', '<a><span>'); * echo $text->createExcerpt(); * */ class text{ public $text; public $length; public $allowedTags; public $readMoreText; //create internal variables from those passed to the class function __construct($text = '', $length = 50, $readMoreText = 'Read In Full...', $before = '', $after = '', $allowedTags = '<a>') { $this->text = $text; $this->length = $length; $this->readMoreText = $readMoreText; $this->before = $before; $this->after = $after; $this->allowedTags = $allowedTags; } //shorten text to required length public function shortenText() { //explode the string into an array of the length we want if (count(explode(' ', $this->text)) > $this->length) { $textArray = explode(' ', $this->text, $this->length); //remove the excess string from the end of the array array_pop($textArray); //create a string from the array $textString = implode(' ', $textArray); //cleaning up unset($textArray); //return return $textString . '... '; } else { return $this->text; } } //remove tags and other. public function cleanText($text){ //strip the text of html tags, except those we want $strippedText = strip_tags($text, $this->allowedTags); //remove [] and their content $strippedText = preg_replace('`\[[^\]]*\]`','',$strippedText); //return return $strippedText; } //create output public function createExcerpt(){ global $post; //create output $output = ''; $output .= $this->cleanText($this->shortenText()); if($this->readMoreText !== '') { $output .= $this->before; $output .= ' <a href="'. $post->guid .'" title="'. $this->readMoreText . ': '. $post->post_title .'">'. $this->readMoreText . '</a>'; $output .= $this->after; } return $output; } }//end class//
Initial URL
Initial Description
Create excerpts from text block from your wordpress content and add links page / post from the guid.
Initial Title
Wordpress: Text Excerpts with PHP
Initial Tags
php, wordpress, text
Initial Language
PHP