note 63996 modified in function.str-word-count by bobby

From: Date: Fri, 06 Oct 2006 18:14:08 +0000
Subject: note 63996 modified in function.str-word-count by bobby
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-118148@lists.php.net to get a copy of this message
There is a small bug in the "trim_text" function by "webmaster at joshstmarie dot com" below. If the string's word count is lesser than or equal to $truncation, that function will cut off the last word in the string. [EDITOR'S NOTE: above referenced note has been removed] This fixes the problem: <?php function trim_text_fixed($string, $truncation = 250) { $matches = preg_split("/\s+/", $string, $truncation + 1); $sz = count($matches); if ( $sz > $truncation ) { unset($matches[$sz-1]); return implode(' ',$matches); } return $string; } ?> --was-- There is a small bug in the "trim_text" function by "webmaster at joshstmarie dot com" below. If the string's word count is lesser than or equal to $truncation, that function will cut off the last word in the string. This fixes the problem: <?php function trim_text_fixed($string, $truncation = 250) { $matches = preg_split("/\s+/", $string, $truncation + 1); $sz = count($matches); if ( $sz > $truncation ) { unset($matches[$sz-1]); return implode(' ',$matches); } return $string; } ?> http://php.net/manual/en/function.str-word-count.php

« previous php.notes (#118148) next »