note 55865 modified in function.str-word-count by danbrown
| From: | danbrown@php.net | Date: | Fri, 31 Aug 2012 16:28:31 +0000 |
| Subject: | note 55865 modified in function.str-word-count by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190374@lists.php.net to get a copy of this message | ||
Here's a function that will trim a $string down to a certian number of words, and add a...
on the end of it. (explansion of muz1's 1st 100 words
code) ---------------------------------------------- <?php function
trim_text($text, $count){ $text = str_replace(" ", "
", $text); $string = explode(" ",
$text); for ( $wordCounter = 0; $wordCounter <= $count;wordCounter++ ){
$trimed .= $string[$wordCounter]; if ( $wordCounter <
$count ){ $trimed .= " "; } else { $trimed .=
"..."; } } $trimed =
trim($trimed); return
$trimed; } ?> Usage ------------------------------------------------ <?php $string
= "one two three four"; echo trim_text($string,
3); ?> returns: one two
three...
--was--
Here's a function that will trim a $string down to a certian number of words, and add a... on
the end of it.
(explansion of muz1's 1st 100 words code)
----------------------------------------------
function trim_text($text, $count){
$text = str_replace(" ", " ", $text);
$string = explode(" ", $text);
for ( $wordCounter = 0; $wordCounter <= $count;wordCounter++ ){
$trimed .= $string[$wordCounter];
if ( $wordCounter < $count ){ $trimed .= " "; }
else { $trimed .= "..."; }
}
$trimed = trim($trimed);
return $trimed;
}
Usage
------------------------------------------------
$string = "one two three four";
echo trim_text($string, 3);
returns:
one two three...
http://php.net/manual/en/function.str-word-count.php