Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- //Source: http://www.nutt.net/2004/12/29/php-a-function-to-return-the-first-n-words-from-a-string/
- }
Advertisement
Add Comment
Please, Sign In to add comment