JBZoo\Utils\Str::limitChars PHP 메소드

limitChars() 공개 정적인 메소드

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 '...'
리턴 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

예제 #1
0
파일: StringTest.php 프로젝트: jbzoo/utils
 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));
 }