From b0fba5798b576e05de0654c0414a7f6df9b3e39b Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Mon, 8 Jan 2018 23:13:49 +0100 Subject: Bugfix: Incorrect behavior of the "truncate" function in some circumstances: If the first word from $string was longer than $length characters, then the regular expression had not truncated the string at all (for example, a string with a total length of 2000 characters was displayed without any cut, but with the $replace string at the end). This bug has been reported by Markus Hackspacher (https://github.com/MarkusHackspacher). --- core/functions.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/core/functions.php b/core/functions.php index 36d87d5..ad63334 100644 --- a/core/functions.php +++ b/core/functions.php @@ -232,10 +232,13 @@ function getRandomValue($length = 40): string { #=============================================================================== function truncate($string, $length, $replace = '') { if(mb_strlen($string) > $length) { - $truncated = preg_replace("/^(.{1,{$length}}\\b).*/su", '$1', $string); + $truncated = preg_replace("/^(.{0,{$length}}\\b).*/su", '$1', $string); $truncated = trim($truncated); - return "{$truncated}{$replace}"; + # The additional trim call is useful, because if $truncated is empty, + # then there will be an unnecessary space between those two variables + # if $replace is preceded by a space (for example: " […]"). + return trim("{$truncated}{$replace}"); } return $string; -- cgit v1.2.3