From 2d87b4d2fa7d2fd01876eaa8cf1f727d863d3e6c Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Wed, 30 Jan 2008 15:19:53 +0000 Subject: Fixed a bug (#1872) where word_limiter() was not retaining whitespace. --- system/helpers/text_helper.php | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) (limited to 'system/helpers/text_helper.php') diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 30cec3148..21ab77830 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -40,27 +40,21 @@ */ if (! function_exists('word_limiter')) { - function word_limiter($str, $n = 100, $end_char = '…') + function word_limiter($str, $limit = 100, $end_char = '…') { - if (strlen($str) < $n) + if (trim($str) == '') { return $str; } - $words = explode(' ', preg_replace("/\s+/", ' ', preg_replace("/(\r\n|\r|\n)/", " ", $str))); - - if (count($words) <= $n) - { - return $str; - } + preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches); - $str = ''; - for ($i = 0; $i < $n; $i++) + if (strlen($str) == strlen($matches[0])) { - $str .= $words[$i].' '; + $end_char = ''; } - - return trim($str).$end_char; + + return rtrim($matches[0]).$end_char; } } -- cgit v1.2.3-24-g4f1b