Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
TINY TRUNCATE WORD FUNCTION
<?php
/*****************************| TRUNCATE WORDS |*****************************/
/**
* TINY TRUNCATE WORD FUNCTION
*
* @param string $t String to be shortened
* @param int $count Number of words to output
* @param boolen $dots Add dots at the end with string is larger than $count. (optional) [default=true]
*
* @return string of $count words with added dots
*/
function trancateWords($t, $count, $dots = true) {
$words = implode(" ", array_slice( explode(" ", $t), 0, $count) );
if( strlen($t) > strlen($words) && $dots ) $words .= '...';
return $words;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.