JBZoo\Utils\Str::limitChars PHP Method

limitChars() public static method

Truncate the string to given length of characters.
public static limitChars ( string $string, integer $limit = 100, string $append = '...' ) : string
$string string The variable to truncate
$limit integer The length to truncate the string to
$append string Text to append to the string IF it gets truncated, defaults to '...'
return string
    public static function limitChars($string, $limit = 100, $append = '...')
    {
        if (self::len($string) <= $limit) {
            return $string;
        }
        return rtrim(self::sub($string, 0, $limit)) . $append;
    }

Usage Example

Example #1
0
 public function testLimitChars()
 {
     is('The quick brown fox jump...', Str::limitChars('The quick brown fox jumps over the lazy dog', 24));
     is('The quick brown fox jumps over the lazy dog', Str::limitChars('The quick brown fox jumps over the lazy dog', 55));
     is('Th...', Str::limitChars('The quick brown fox jumps over the lazy dog', 2));
     is('The...', Str::limitChars('The quick brown fox jumps over the lazy dog', 3));
     is('The qui...', Str::limitChars('The quick brown fox jumps over the lazy dog', 7));
     is('The quick brown fox jumps over the lazy dog', Str::limitChars('The quick brown fox jumps over the lazy dog', 150));
 }