/**
* Strip text longer then given length
* @param string $text Text to be stripped
* @param int $length Length of chars to keep
* @param string $glueChar (optional) Show when length exceeded
* @return string|null Stripped text | Text was empty
*/
function getCharsByLength ( $text, $length, $glueChar = '...' ) {
if ( !trim ($text ) ) {
return null;
}
/** @var int $actualLength */
$actualLength = function_exists('mb_strlen')
? mb_strlen($text)
: strlen($text);
// desired length is same or less, return as it is
if ( $actualLength <= $length ) {
return $text;
}
return function_exists ( 'mb_substr' )
? mb_substr ($text, 0, $length, 'utf8' ) . $glueChar
: substr ($text, 0, $length ) . $glueChar;
}
/************
* EXAMPLE
***********/
getCharsByLength ('Νο εαμ σθμμο vολθτπατ. Εα εστ ομνεσ ιμπερδιετ εφφιcιαντθρ', 10);
/***********
* OUTPUT
**********/
# Νο εαμ σθμ...
Чтобы увидеть комментарии, нужно быть участником сообщества
Регистрация