note 48877 deleted from function.explode by bjori

From: Date: Mon, 25 Sep 2006 08:11:01 +0000
Subject: note 48877 deleted from function.explode by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-117596@lists.php.net to get a copy of this message
Note Submitter: marcyboy45 at hotmail dot com ---- I'm not going for the noble prize with this one, but it saves you having to writing something similar if the occasion ever presents itself! This is just a simple function to cut short a paragraph like what you'd see in the results of a web search. You can specify the number of words and the ellipsis which makes it quite flexible. <?php function wordlimit($string, $length = 50, $ellipsis = "...") { $paragraph = explode(" ", $string); if($length < count($paragraph)) { for($i = 0; $i < $length; $i++) { if($i < $length - 1) $output .= $paragraph[$i] . " "; else $output .= $paragraph[$i] . $ellipsis; } return $output; } return $string; } ?> An example would look like: <?php $string = "This is a very simple function, but nifty nonetheless."; print wordlimit($string, 5); // This is a very simple... ?>

« previous php.notes (#117596) next »