note 53876 deleted from function.explode by betz

From: Date: Sat, 11 Feb 2006 16:40:57 +0000
Subject: note 53876 deleted from function.explode by betz
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-103511@lists.php.net to get a copy of this message
Note Submitter: contact_trieu at hotmail dot com ---- Here is a modified version of the function below. Rather than limit by number of words, this should limit by number of characters. The function will cut off the sentence to the preceding word taking into consideration the ending "ellipsis". function charLimit($string, $length = 25, $ellipsis = "...") { $words = explode(' ', $string); $curindex = 0; $wcount = 0; foreach($words as $value){ $wcount += strlen($value); if($wcount>$length-strlen($ellipsis)){ break; } $curindex++; } return implode(' ', array_slice($words, 0, $curindex)) . $ellipsis; }

« previous php.notes (#103511) next »