Returns "This is a..." echo Str::words('This is a sentence.', 3); Limit the number of words and append a custom ending echo Str::words('This is a sentence.', 3, '---');
public function words($value, $words = 100, $end = '...') { if (trim($value) == '') { return ''; } preg_match('/^\\s*+(?:\\S++\\s*+){1,' . $words . '}/u', $value, $matches); if ($this->length($value) == $this->length($matches[0])) { $end = ''; } return rtrim($matches[0]) . $end; }