Created
May 22, 2013 10:50
-
-
Save rajvanshipradeep15/5626728 to your computer and use it in GitHub Desktop.
wordpress: truncatewords
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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