Laravel Excerpt

Laravel Excerpt

The Laravel framework provides a convenient method to generate excerpts from a given text. The `Str::words()` method can be used to generate a summary or an excerpt by truncating the text to a specific number of words.

Syntax:

$excerpt = Str::words($text, $words, $end = '...');

Parameters:

  • $text: The original text or content from which an excerpt needs to be extracted.
  • $words: The number of words to include in the excerpt.
  • $end (optional): The string to be appended at the end of the excerpt. By default, it is set to ‘…’.

Example:

$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed fringilla, lectus ut tempor condimentum, lacus orci rhoncus mi, nec gravida purus nisi quis ex. Phasellus dignissim libero eget eros ultrices, non tempor nunc tincidunt. Aliquam hendrerit vestibulum orci id mattis.";
  
$excerpt = Str::words($text, 10, '...');

echo $excerpt;

In this example, the original text is truncated to generate an excerpt containing 10 words. The resulting excerpt would be:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed fringilla,…

The generated excerpt can then be displayed, for example, in a preview or a summary section of an article or content.

Read more

Leave a comment