summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2017-11-29 00:58:59 +0100
committerThomas Lange <code@nerdmind.de>2017-11-29 00:58:59 +0100
commitebb890d0c6280653360045d13ca233b68697be2b (patch)
tree2b26ae168b57794a6cb1d23e7ef0daf8222bd583
parent552fb9720ebba066b9b983a6415f7195e123216b (diff)
downloadblog-ebb890d0c6280653360045d13ca233b68697be2b.tar.gz
blog-ebb890d0c6280653360045d13ca233b68697be2b.tar.xz
blog-ebb890d0c6280653360045d13ca233b68697be2b.zip
The "cut" function has been optimized and renamed to "truncate". In addition, the $replace parameter is now an empty string by default.
-rw-r--r--core/functions.php15
1 files changed, 8 insertions, 7 deletions
diff --git a/core/functions.php b/core/functions.php
index 1f7bfbb..36d87d5 100644
--- a/core/functions.php
+++ b/core/functions.php
@@ -228,13 +228,14 @@ function getRandomValue($length = 40): string {
}
#===============================================================================
-# Return cutted string
+# Return truncated string
#===============================================================================
-function cut($string, $length, $replace = ' […]') {
+function truncate($string, $length, $replace = '') {
if(mb_strlen($string) > $length) {
- return preg_replace_callback("/^(.{1,{$length}}\\b).*/su", function($match) {
- return trim($match[1]);
- }, $string).$replace;
+ $truncated = preg_replace("/^(.{1,{$length}}\\b).*/su", '$1', $string);
+ $truncated = trim($truncated);
+
+ return "{$truncated}{$replace}";
}
return $string;
@@ -246,7 +247,7 @@ function cut($string, $length, $replace = ' […]') {
function excerpt($string, $length = 500, $replace = ' […]') {
$string = removeHTML($string);
$string = removeDoubleLineBreaks($string);
- $string = cut($string, $length, $replace);
+ $string = truncate($string, $length, $replace);
$string = nl2br($string);
return $string;
@@ -258,7 +259,7 @@ function excerpt($string, $length = 500, $replace = ' […]') {
function description($string, $length = 200, $replace = ' […]') {
$string = removeHTML($string);
$string = removeWhitespace($string);
- $string = cut($string, $length, $replace);
+ $string = truncate($string, $length, $replace);
return $string;
}