diff options
author | Derek Jones <derek.jones@ellislab.com> | 2008-01-30 16:19:53 +0100 |
---|---|---|
committer | Derek Jones <derek.jones@ellislab.com> | 2008-01-30 16:19:53 +0100 |
commit | 2d87b4d2fa7d2fd01876eaa8cf1f727d863d3e6c (patch) | |
tree | 184bacf2c551f34a1011c77576087892b2cdd09a /system/helpers/text_helper.php | |
parent | 8f61766d1d9a6ee9e4d462cccaaf4753d46a71de (diff) |
Fixed a bug (#1872) where word_limiter() was not retaining whitespace.
Diffstat (limited to 'system/helpers/text_helper.php')
-rw-r--r-- | system/helpers/text_helper.php | 20 |
1 files changed, 7 insertions, 13 deletions
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;
}
}
|