note 44969 added to function.substr
| From: | JamieLongstaffinfoATpopsoftDOTinfo at osu1 dot php dot net | Date: | Sat, 21 Aug 2004 15:45:04 +0000 |
| Subject: | note 44969 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-75064@lists.php.net to get a copy of this message | ||
This function is a rough and ready hack to take a string and trim it to a certain word length. A
trailing ellipsis is added if the string is truncated. If the number of words in the string is
shorter than the specified length, the string is returned unaltered.
<?
## Take a sentence or group of words and trim to a defined word length with closing ellipis (...)
##
## $string (string) The string to truncate
## $length (int) The maximum number of words to output
function trim_words($string,$length) {
$words = explode(" ",$string);
if (count($words >= $length)) {
$trimmed = "";
for ($i = 0; $i < $length; $i++) {
$trimmed .= $words[$i]." ";
}
$trimmed .= "...";
return($trimmed);
} else {
return($string);
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+44969
Delete -- http://master.php.net/manage/user-notes.php?action=delete+44969&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+44969&report=yes
Search -- http://master.php.net/manage/user-notes.php