Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ferlyz05/8064148 to your computer and use it in GitHub Desktop.
Save ferlyz05/8064148 to your computer and use it in GitHub Desktop.
function shorten_string($string, $wordsreturned)
/* Returns the first $wordsreturned out of $string. If string
contains fewer words than $wordsreturned, the entire string
is returned.
*/
{
$retval = $string; // Just in case of a problem
$array = explode(" ", $string);
if (count($array)<=$wordsreturned)
/* Already short enough, return the whole thing */
{
$retval = $string;
}
else
/* Need to chop of some words */
{
array_splice($array, $wordsreturned);
$retval = implode(" ", $array)." ...";
}
return $retval;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment