excerpt in characters

I have code in functions.php:

function string_limit_words($string, $word_limit)
{
  $words = explode(' ', $string, ($word_limit + 1));
  if(count($words) > $word_limit)
  array_pop($words);
  return implode(' ', $words);
}

but i need to limit excerpt in number of characters,
could you help me with that?

4 s
4

I used this code in one of my last projects:

function ng_get_excerpt( $count ){
  $permalink = get_permalink( $post->ID );
  $excerpt = get_the_content();
  $excerpt = strip_tags( $excerpt );
  $excerpt = mb_substr( $excerpt, 0, $count );
  $excerpt = mb_substr( $excerpt, 0, strripos( $excerpt, " " ) );
  $excerpt = rtrim( $excerpt, ",.;:- _!$&#" );
  $excerpt = $excerpt . '<a href="'.$permalink.'" style="text-decoration: none;">&nbsp;(...)</a>';
  return $excerpt;
}

I got it from here:

http://wordpress.org/support/topic/limit-excerpt-length-by-characters

https://stackoverflow.com/questions/10923955/make-function-that-limits-text-not-show-last-punctuation-mark

It has the advantage of not allowing punctuation on the end and ending with the last complete word

Using the filters as suggested by @medhamza7 or @bainternet or @fuxia is preferable.

Leave a Comment