Truncate string in Laravel blade templates
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:
{{ 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 ''
.
{{ str_limit($string, $limit, '') }}
Watch a video course
Learn object oriented PHP
If you want to truncate the string to the nearest word, you can pass a fourth argument as true
.
{{ str_limit($string, $limit, '', true) }}
This will truncate the string to the nearest whole word that is less than or equal to the specified number of characters.