/**
  * Sets the length for the excerpt,
  * then it adds the WP filter
  * And automatically calls the_excerpt();
  *
  * @param string $new_length 
  * @return void
  *
  */
 public static function length($new_length = 55)
 {
     Excerpt::$length = $new_length;
     add_filter('excerpt_length', 'Excerpt::new_length', 999);
     add_filter('excerpt_more', 'Excerpt::excerpt_more', 999);
     Excerpt::output();
 }
 public function __construct($data)
 {
     parent::__construct($data);
     // Get the search term
     $search_term = get_query_var('s');
     // Sanitize the search term
     $key = esc_html($search_term, 1);
     // Number of characters before the highlighted text.
     $this->chars_before = isset($data['chars_before']) ? $data['chars_before'] : 100;
     // Excerpt total characters
     $this->chars_total = isset($data['chars_total']) ? $data['chars_total'] : 250;
     // Retrieve content and strip out all HTML
     $content = strip_shortcodes(strip_tags(get_the_content()));
     $content = preg_replace('/\\b(https?):\\/\\/[-A-Z0-9+&@#\\/%?=~_|$!:,.;]*[A-Z0-9+&@#\\/%=~_|$]/i', '', $content);
     $content = preg_replace('|www\\.[a-z\\.0-9]+|i', '', $content);
     // Get the position of the key inside of the content.
     $key_position = stripos($content, $key);
     $start = 0;
     $length = $this->chars_total;
     $before = '';
     $after = ' …';
     // If the key position is somewhere inside the content,
     // the starting position is calculated based on the charsBefore value,
     // and the SearchExcerpt needs a ellipsis prepended to it.
     if ($key_position >= $this->chars_before) {
         $start = $key_position - $this->chars_before;
         $before = '… ';
     }
     // If our projected length is longer than the content string, then we don't need an ellipsis afterward,
     // and the length of the substr needs to be adjusted.
     if ($start + $this->chars_total > strlen($content)) {
         $length = strlen($content) - $key_position;
         $after = '';
     }
     // Get the part of the content that we'll use for the SearchExcerpt
     $search_excerpt_raw = substr($content, $start, $length);
     // Find matches for the search term.
     preg_match_all("/{$key}+/i", $search_excerpt_raw, $matches);
     $search_excerpt_highlights = '';
     // If we have matches (we should), add a span to each match for special styles.
     if (is_array($matches[0]) && count($matches[0]) >= 1) {
         foreach ($matches[0] as $match) {
             $search_excerpt_highlights = str_replace($match, '<strong class="highlighted">' . $match . '</strong>', $search_excerpt_raw);
         }
     }
     // Build the search excerpt.
     $search_excerpt = $before . $search_excerpt_highlights . $after;
     $this->content = $search_excerpt;
 }
Example #3
0
<?php

$plugin_info = array('pi_name' => 'Excerpt', 'pi_version' => '1.0', 'pi_author' => 'Hayden Hancock', 'pi_author_url' => 'http://haydenhancock.com', 'pi_description' => 'The excerpt word amount will be 55 words and if the amount is greater than that, then the string "..." will be appended to the excerpt. If the string is less than 55 words, then the content will be returned as is. The 55 word limit can be modified by using the excerpt_length parameter. The "..." string can be modified by using the excerpt_more parameter.', 'pi_usage' => Excerpt::usage());
class Excerpt
{
    var $return_data;
    function excerpt()
    {
        $this->EE =& get_instance();
        $text = $this->EE->TMPL->tagdata;
        // Parameter(s)
        $excerpt_length = $this->EE->TMPL->fetch_param('excerpt_length');
        $excerpt_more = $this->EE->TMPL->fetch_param('excerpt_more');
        // Set defaults
        if (!is_numeric($excerpt_length)) {
            $excerpt_length = 55;
        }
        if ($excerpt_more == '') {
            $excerpt_more = '...';
        }
        $this->return_data = $this->trim_excerpt($text, $excerpt_length, $excerpt_more);
    }
    function trim_excerpt($text, $excerpt_length, $excerpt_more)
    {
        if (strlen($text) < $excerpt_length) {
            return $text;
        }
        $words = explode(' ', preg_replace("/\\s+/", ' ', preg_replace("/(\r\n|\r|\n)/", " ", $text)));
        if (count($words) <= $excerpt_length) {
            return $text;
        }
 public function __construct($data)
 {
     parent::__construct($data);
     $this->content = get_the_excerpt();
 }
 function __construct($length)
 {
     Excerpt::$length = $length;
     add_filter('excerpt_length', array('Excerpt', 'new_length'));
 }
<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}
$plugin_info = array('pi_name' => 'Excerpt', 'pi_version' => '1.0.2', 'pi_author' => 'Clayton McIlrath', 'pi_author_url' => 'http://thinkclay.com/', 'pi_description' => 'Limits the number of words in some text, after stripping tags.', 'pi_usage' => Excerpt::usage());
/**
 * Excerpt Class
 *
 * @package			ExpressionEngine
 * @category		Plugin
 * @author			Clayton McIlrath
 * @copyright		Copyright (c) 2010, Chosen, LLC.
 * @link			NA
 */
class Excerpt
{
    var $return_data;
    function __construct()
    {
        $this->EE =& get_instance();
        $this->indicator = $this->EE->TMPL->fetch_param('indicator', '');
        $this->limit = $this->EE->TMPL->fetch_param('limit', 500);
        $this->limit_type = $this->EE->TMPL->fetch_param('limit_type', 'words');
        // cleanup limit parameter
        if (!is_numeric($this->limit)) {
            $this->EE->TMPL->log_item('Excerpt: Error - limit parameter not numeric');
            $this->limit = 500;
        }
        // cleanup limit_type parameter
        if (in_array($this->limit_type, array('words', 'chars')) == FALSE) {