From 1f0e58d6c26dce5bf82d0142b6f210fcf32c281d Mon Sep 17 00:00:00 2001 From: Manfred Kindl Date: Mon, 20 Nov 2017 18:28:18 +0100 Subject: [PATCH] Add funktion cutString Cuts the string to the given limit minus the stringlength of the placeholderSign and adds the placeholderSign at the end of the string --- include/functions.inc.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/include/functions.inc.php b/include/functions.inc.php index 036b9d1a3..5fc5e5418 100644 --- a/include/functions.inc.php +++ b/include/functions.inc.php @@ -1043,4 +1043,33 @@ function generateSpecialCharacterString($inputString, $punctuationMark = false) return $inputString; } + +/** + * Cuts the string to the given limit minus the stringlength of the placeholderSign and adds the placeholderSign at the end of the string + * If $keepFilextension is true, the string is checked for a PATHINFO_EXTENSION and the extension is added to the returned string. + * @param string $string The input string to be cutted + * @param integer $limit The length of the returned string (including the placeholderSigns) + * @param string $placeholderSign Optional. Default null. The string to be added at the end of the cutted string. + * @param bool $keepFilextension. Default false. When set to true the + * @return string The cutted string with the placeholderSign at the end + */ +function cutString($string, $limit, $placeholderSign = '', $keepFilextension = false) +{ + $offset = strlen($placeholderSign); + $extension = ''; + if ($keepFilextension) + { + $extension = '.'.pathinfo($string, PATHINFO_EXTENSION); + $offset = $offset + strlen($extension); + } + + if(strlen($string) > ($limit - $offset)) + { + return substr($string, 0, ($limit - $offset)).$placeholderSign.$extension; + } + else + { + return false; + } +} ?>