note 48877 added to function.explode

From: Date: Mon, 10 Jan 2005 13:04:21 +0000
Subject: note 48877 added to function.explode
Groups: php.notes 
Request: Send a blank email to php-notes+get-83044@lists.php.net to get a copy of this message
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... ?> ---- Manual Page -- http://www.php.net/manual/en/function.explode.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+48877 Delete -- http://master.php.net/manage/user-notes.php?action=delete+48877&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48877&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48877&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48877&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48877&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48877&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+48877&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#83044) next »