note 63996 deleted from function.str-word-count by danbrown
| From: | danbrown@php.net | Date: | Fri, 31 Aug 2012 16:27:54 +0000 |
| Subject: | note 63996 deleted from function.str-word-count by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190372@lists.php.net to get a copy of this message | ||
Note Submitter: rabin at rab dot in
----
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;
}
?>