note 89907 added to function.preg-split

From: Date: Fri, 27 Mar 2009 19:02:55 +0000
Subject: note 89907 added to function.preg-split
Groups: php.notes 
Request: Send a blank email to php-notes+get-152542@lists.php.net to get a copy of this message
how to display a shortened text string with an elipsis, but on word boundaries only. function truncate($string, $max = 70, $rep = '...') { $words = preg_split("/[\s]+/", $string); $newstring = ''; $numwords = 0; foreach ($words as $word) { if ((strlen($newstring) + 1 + strlen($word)) < $max) { $newstring .= ' '.$word; ++$numwords; } else { break; } } if ($numwords < count($words)) { $newstring .= $rep; } return $newstring; } hope this helps someone! thanks for all the help from everyone else!! ---- Server IP: 208.69.120.35 Probable Submitter: 69.69.18.124 ---- Manual Page -- http://www.php.net/manual/en/function.preg-split.php Edit -- https://master.php.net/note/edit/89907 Del: integrated -- https://master.php.net/note/delete/89907/integrated Del: useless -- https://master.php.net/note/delete/89907/useless Del: bad code -- https://master.php.net/note/delete/89907/bad+code Del: spam -- https://master.php.net/note/delete/89907/spam Del: non-english -- https://master.php.net/note/delete/89907/non-english Del: in docs -- https://master.php.net/note/delete/89907/in+docs Del: other reasons-- https://master.php.net/note/delete/89907 Reject -- https://master.php.net/note/reject/89907 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#152542) next »