lib: Add String::ellipsis()

This commit is contained in:
Eric Lippmann 2015-01-19 14:20:37 +01:00
parent f56ffd3426
commit a0a3241d1c
1 changed files with 18 additions and 0 deletions

View File

@ -36,4 +36,22 @@ class String
{ {
return str_replace(' ', '', ucwords(str_replace($separator, ' ', strtolower($name)))); 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;
}
} }