Lime\Helper\Utils::safe_truncate PHP Метод

safe_truncate() публичный статический Метод

Truncate a string to a specified length without cutting a word off.
public static safe_truncate ( string $string, integer $length, string $append = '...' ) : string
$string string The string to truncate
$length integer The length to truncate the string to
$append string Text to append to the string IF it gets truncated, defaults to '...'
Результат string
    public static function safe_truncate($string, $length, $append = '...')
    {
        $ret = substr($string, 0, $length);
        $last_space = strrpos($ret, ' ');
        if ($last_space !== false && $string != $ret) {
            $ret = substr($ret, 0, $last_space);
        }
        if ($ret != $string) {
            $ret .= $append;
        }
        return $ret;
    }