str::short PHP Method

short() static public method

Shortens a string and adds an ellipsis if the string is too long
static public short ( string $string, integer $chars, string $rep = '…' ) : string
$string string The string to be shortened
$chars integer The final number of characters the string should have
$rep string The element, which should be added if the string is too long. Ellipsis is the default.
return string The shortened string
    static function short($string, $chars, $rep = '…')
    {
        if ($chars == 0) {
            return $string;
        }
        if (str::length($string) <= $chars) {
            return $string;
        }
        $string = self::substr($string, 0, $chars - str::length($rep));
        $punctuation = '.!?:;,-';
        $string = strspn(strrev($string), $punctuation) != 0 ? substr($string, 0, -strspn(strrev($string), $punctuation)) : $string;
        return $string . $rep;
    }

Usage Example

Ejemplo n.º 1
0
function truncateString($text, $length)
{
    ob_start();
    $short = \str::short($text, $length);
    ob_end_clean();
    return $short;
}
All Usage Examples Of str::short