From 3e869fd60d2fd47ca224c169cca4095f20babc27 Mon Sep 17 00:00:00 2001 From: Hakim Zulkufli Date: Sun, 6 Aug 2017 02:34:32 +0800 Subject: [PATCH] More safety checks for Text::truncate --- bl-kernel/helpers/text.class.php | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/bl-kernel/helpers/text.class.php b/bl-kernel/helpers/text.class.php index a165ad6b..f2e6de04 100644 --- a/bl-kernel/helpers/text.class.php +++ b/bl-kernel/helpers/text.class.php @@ -216,23 +216,22 @@ class Text { // Truncates the string under the limit specified by the limit parameter. public static function truncate($string, $limit, $end = '...') { - - // Check if string is only one word - if(preg_match('/\s/', $string)) { + // Check if over $limit + if(mb_strlen($string) > $limit) { - // Append the string specified by the end parameter to the end of the string as it is over the limit. - $truncate = trim(mb_substr($string, 0, mb_strpos($string, ' ', $limit, 'UTF-8'), 'UTF-8')).$end; - - } else { - $truncate = trim(mb_substr($string, 0, $limit, 'UTF-8')); - - // Check if string is more than limit - if(mb_strlen($string) > $limit) { - // Append $end - $truncate = $truncate.$end; + // Check if string is only one word + if(preg_match('/\s/', $string)) { + + // Append the string specified by the end parameter to the end of the string as it is over the limit. + $truncate = trim(mb_substr($string, 0, mb_strpos($string, ' ', $limit, 'UTF-8'), 'UTF-8')); + } else { + $truncate = trim(mb_substr($string, 0, $limit, 'UTF-8')); } + $truncate = $truncate.$end; + } else { + $truncate = $string; } - + if(empty($truncate)) { return ''; }