From a0a3241d1c42d138a588478f4199d3cdcd887d75 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 19 Jan 2015 14:20:37 +0100 Subject: [PATCH] lib: Add String::ellipsis() --- library/Icinga/Util/String.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/library/Icinga/Util/String.php b/library/Icinga/Util/String.php index 248ab0d34..69fb26927 100644 --- a/library/Icinga/Util/String.php +++ b/library/Icinga/Util/String.php @@ -36,4 +36,22 @@ class String { return str_replace(' ', '', ucwords(str_replace($separator, ' ', strtolower($name)))); } + + /** + * Add ellipsis when a string is longer than max length + * + * @param string $string + * @param int $maxLength + * @param string $ellipsis + * + * @return string + */ + public static function ellipsis($string, $maxLength, $ellipsis = '...') + { + if (strlen($string) > $maxLength) { + return substr($string, 0, $maxLength - strlen($ellipsis)) . $ellipsis; + } + + return $string; + } }