note 44969 deleted from function.substr by aidan
| From: | aidan@php.net | Date: | Sun, 22 Aug 2004 11:17:22 +0000 |
| Subject: | note 44969 deleted from function.substr by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-75093@lists.php.net to get a copy of this message | ||
Note Submitter: Jamie Longstaff info AT popsoft DOT info
----
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);
}
}
?>