How to Truncate String in Laravel
BY: DA PUBLISHED: 2024-03-31 17:09:25 | UPDATED: 2025-02-21 18:03:32 | Views: 581
To truncate string in Laravel. Laravel 7

Laravel 7 offers a more object-oriented, fluent string manipulation library built on top
of Illuminate\Support\Str functions. Let’s see a few examples:
Table of contents
Truncate in controller
Using limit function, we can remove characters from string:
use Illuminate\Support\Str; $truncated = Str::of('The quick brown fox jumps over the lazy dog.')->limit(20); dd($truncated); // The quick brown fox...
We can replace … with any custom character. Let’s replace ... with >>>
use Illuminate\Support\Str; $truncated = Str::of('The quick brown fox jumps over the lazy dog.')->limit(20, ' >>>'); dd($truncated); // The quick brown fox >>>
We can also truncate words with words method:
use Illuminate\Support\Str; $truncated = Str::of('The quick brown fox jumps over the lazy dog.')->words(4); dd($truncated); // The quick brown fox...
Truncate in Blade
Remove characters from string in blade:
{!! Str::limit('The quick brown fox jumps over the lazy dog.', 20) !!} <!--The quick brown fox...-->
Truncate words in blade:
{!! Str::words('The quick brown fox jumps over the lazy dog.', 4) !!} <!--The quick brown fox...-->
If we want, we can set namespace too:
{!! \Illuminate\Support\Str::words('The quick brown fox jumps over the lazy dog.', 4) !!} <!--The quick brown fox...-->
That’s all, artisans. Thanks for reading.