Skip to content

Instantly share code, notes, and snippets.

@CodeNegar
Created September 13, 2012 11:55
Show Gist options
  • Save CodeNegar/3713863 to your computer and use it in GitHub Desktop.
Save CodeNegar/3713863 to your computer and use it in GitHub Desktop.
PHP: limit words
<?php
function limit_words($string, $word_limit) {
$string = strip_tags($string);
$words = explode(' ', strip_tags($string));
$return = trim(implode(' ', array_slice($words, 0, $word_limit)));
if(strlen($return) < strlen($string)){
$return .= '...';
}
return $return;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment