note 34929 added to ref.strings
| From: | strafe{at}strafeonline dot com at rack1 dot php dot net | Date: | Tue, 12 Aug 2003 13:16:34 +0000 |
| Subject: | note 34929 added to ref.strings | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-54005@lists.php.net to get a copy of this message | ||
Based on szeryf at negativeIQ dot pl's post on 02-Apr-2003 05:41, regarding truncating strings
to the nearest whitespace.. I've come up with a slightly modified version.
The original version would stop at a newline and hence not output the specified number of
characters.
function truncate ($var, $len = 120) {
if (empty ($var)) { return ""; }
if (strlen ($var) < $len) { return $var; }
if (preg_match ("/(.{1,$len})\s./ms", $var, $match)) { return $match [1] .
"..."; }
else { return substr ($var, 0, $len) . "..."; }
}
The only difference is with the regular expression, from:
"/(.{1,$len})\s/"
to:
"/(.{1,$len})\s./ms"
Hopefully this is helpful for someone other than myself. :-)
----
Manual Page -- http://www.php.net/manual/en/ref.strings.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34929
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34929&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34929&report=yes