note 34929 deleted from ref.strings by aidan

From: Date: Tue, 28 Sep 2004 12:11:58 +0000
Subject: note 34929 deleted from ref.strings by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-77493@lists.php.net to get a copy of this message
Note Submitter: strafe{at}strafeonline.com ---- 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. :-)

« previous php.notes (#77493) next »