W3docs

Truncate string in Laravel blade templates

To truncate a string in a Laravel blade template, you can use the str_limit function.

To truncate a string in a Laravel blade template, you can use the str_limit function. This function is provided by Laravel and is designed to truncate strings. Here is an example of how you can use it:

Example of truncating a string in a Laravel blade template

{{ str_limit($string, $limit) }}

This will truncate the string to the specified number of characters and append an ellipsis (...) to the end of the string. If you do not want the ellipsis to be appended, you can pass a third argument as ''.

Example of a truncated string in a Laravel blade template

{{ str_limit($string, $limit, '') }}

If you want to truncate the string to the nearest word, you can use the Str::words() helper instead.

Example of truncating a string to the nearest word in a Laravel blade template

{{ Str::words('This is a long string', 3) }}

This will truncate the string to the nearest whole word that is less than or equal to the specified number of words.