aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2018-01-08 23:13:49 +0100
committerThomas Lange <code@nerdmind.de>2018-01-08 23:13:49 +0100
commitb0fba5798b576e05de0654c0414a7f6df9b3e39b (patch)
tree7fe9e2dae8c1da454f283cd7faf7d6c097a1757a
parentebb890d0c6280653360045d13ca233b68697be2b (diff)
downloadblog-b0fba5798b576e05de0654c0414a7f6df9b3e39b.tar.gz
blog-b0fba5798b576e05de0654c0414a7f6df9b3e39b.tar.xz
blog-b0fba5798b576e05de0654c0414a7f6df9b3e39b.zip
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).
-rw-r--r--core/functions.php7
1 files 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;