diff options
Diffstat (limited to 'system/helpers')
-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;
}
}
|