note 55865 added to function.str-word-count
| From: | MadCoder at osu1 dot php dot net | Date: | Tue, 16 Aug 2005 04:12:56 +0000 |
| Subject: | note 55865 added to function.str-word-count | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-93705@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)
----------------------------------------------
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...
----
Manual Page -- http://www.php.net/manual/en/function.str-word-count.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55865
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55865&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55865&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55865&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55865&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55865&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55865&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55865&report=yes
Search -- http://master.php.net/manage/user-notes.php