note 56836 deleted from function.substr by bobby

From: Date: Thu, 14 Sep 2006 17:45:14 +0000
Subject: note 56836 deleted from function.substr by bobby
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-117184@lists.php.net to get a copy of this message
Note Submitter: bob at 808medien dot de ---- Truncates a string at a certain position without »cutting« words into senseless pieces. I hope this is usefull for some of you and that it is not another redundant note (I didn't find one) ... <?php $string = "Truncates a string at a certain position without &raquo;cutting&laquo; words into senseless pieces."; $maxlength = 75; $extension = " ..."; function truncate_string ($string, $maxlength, $extension) { // Set the replacement for the "string break" in the wordwrap function $cutmarker = "**cut_here**"; // Checking if the given string is longer than $maxlength if (strlen($string) > $maxlength) { // Using wordwrap() to set the cutmarker // NOTE: wordwrap (PHP 4 >= 4.0.2, PHP 5) $string = wordwrap($string, $maxlength, $cutmarker); // Exploding the string at the cutmarker, set by wordwrap() $string = explode($cutmarker, $string); // Adding $extension to the first value of the array $string, returned by explode() $string = $string[0] . $extension; } // returning $string return $string; } // This Will output: // "Truncates a string at a certain position without »cutting« ..." echo truncate_string ($string, $maxlength, $extension); ?> Best Bob

« previous php.notes (#117184) next »