Flow\Helper::truncate PHP Method

truncate() public static method

public static truncate ( $obj = null, $length = 255, $preserve_words = false, $hellip = '…' )
    public static function truncate($obj = null, $length = 255, $preserve_words = false, $hellip = '…')
    {
        $obj = strval($obj);
        $len = strlen($obj);
        if ($length >= $len) {
            return $obj;
        }
        $truncated = $preserve_words ? preg_replace('/\\s+?(\\S+)?$/', '', substr($obj, 0, $length + 1)) : substr($obj, 0, $length);
        return $truncated . $hellip;
    }

Usage Example

Beispiel #1
0
 public function test_truncate()
 {
     $this->assertEquals('this is a …', Helper::truncate('this is a long word', 10));
     $this->assertEquals('this is a', Helper::truncate('this is a long word', 12, true, ''));
     $this->assertEquals('this is a ', Helper::truncate('this is a long word', 10, false, ''));
     $this->assertEquals('this is a ...', Helper::truncate('this is a long word', 10, false, '...'));
 }