Skip to content

Instantly share code, notes, and snippets.

@jayarjo
Created March 18, 2013 10:54
Show Gist options
  • Save jayarjo/5186403 to your computer and use it in GitHub Desktop.
Save jayarjo/5186403 to your computer and use it in GitHub Desktop.
truncate text to a specific word length
function truncate($text, $length, $more = '...')
{
$text = strip_shortcodes( $text );
//$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($text);
$words = explode(' ', $text, $length + 1);
if (count($words) > $length) {
array_pop($words);
$text = implode(' ', $words);
$text = $text . ' ' . $more;
}
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment