note 35474 deleted from function.wordwrap by aidan
| From: | aidan@php.net | Date: | Mon, 19 Jul 2004 23:26:06 +0000 |
| Subject: | note 35474 deleted from function.wordwrap by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-73650@lists.php.net to get a copy of this message | ||
Note Submitter: Neotropic
----
If you are looking for a way to trim a sentence and append, say like ... at the end. I created this
little snipplet that seems to work fine for what I need.
function trim_sentence($string, $num)
{
$done = 0;
$letters = 0;
$sentence = '';
$words = explode(" ", trim($string));
$totalwords = count($words);
for($i = 0; $i < $totalwords; $i++) {
$word_array = preg_split('//', $words[$i], -1, PREG_SPLIT_NO_EMPTY);
$letters = $letters + count($word_array);
if (($letters > $num) && ($done == 0)) {
$sentence = trim($sentence) . "...";
$done = 1;
}
if ($done == 0) {
$sentence .= $words[$i] . " ";
}
}
return ($sentence);
}
It will append to the smallest number. For instance:
$forum = "Wolrds Biggest Ralley";
$forum = trim_sentence($forum, '15');
echo $forum;
Result: Wolrds Biggest...