Created
December 21, 2013 01:09
-
-
Save ferlyz05/8064148 to your computer and use it in GitHub Desktop.
This file contains 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 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