note 35476 added to function.wordwrap
| From: | andy at tagish dot com | Date: | Tue, 02 Sep 2003 13:15:28 +0000 |
| Subject: | note 35476 added to function.wordwrap | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-55467@lists.php.net to get a copy of this message | ||
Neotropic's sentence truncating code is a little ineffecient - there's no need to chop the
sentence into words or indeed chop the words into characters to find out how long they are. This
does the trick a little more succinctly I think:
function truncate($str, $len, $el = '...')
{
if (strlen($str) > $len) {
$xl = strlen($el);
if ($len < $xl) {
return substr($str, 0, $len);
}
$str = substr($str, 0, $len-$xl);
$spc = strrpos($str, ' ');
if ($spc > 0) {
$str = substr($str, 0, $spc);
}
return $str . $el;
}
return $str;
}
----
Manual Page -- http://www.php.net/manual/en/function.wordwrap.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35476
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35476&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35476&report=yes