note 35474 added to function.wordwrap

From: Date: Tue, 02 Sep 2003 09:43:08 +0000
Subject: note 35474 added to function.wordwrap
Groups: php.notes 
Request: Send a blank email to php-notes+get-55463@lists.php.net to get a copy of this message
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... ---- Manual Page -- http://www.php.net/manual/en/function.wordwrap.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35474 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35474&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35474&report=yes

« previous php.notes (#55463) next »