Skip to content

Instantly share code, notes, and snippets.

@dominikwilkowski
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominikwilkowski/8968493 to your computer and use it in GitHub Desktop.
Save dominikwilkowski/8968493 to your computer and use it in GitHub Desktop.
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