Skip to content

Instantly share code, notes, and snippets.

@rajvanshipradeep15
Created May 22, 2013 10:50
Show Gist options
  • Save rajvanshipradeep15/5626728 to your computer and use it in GitHub Desktop.
Save rajvanshipradeep15/5626728 to your computer and use it in GitHub Desktop.
wordpress: truncatewords
function truncateWords($text, $maxLength = 15)
{
// explode the text into an array of words
$wordArray = explode(' ', $text);
// do we have too many?
if( sizeof($wordArray) > $maxLength )
{
// remove the unwanted words
$wordArray = array_slice($wordArray, 0, $maxLength);
// turn the word array back into a string and add our ...
return implode(' ', $wordArray) . '…';
}
// if our array is under the limit, just send it straight back
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment