How to Truncate String in Laravel
BY: DA PUBLISHED: 2024-03-31 16:09:25 | UPDATED: 2026-07-09 17:42:11 | Views: 1152
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.
You May Also Like
5G: What It Means for You
2 years agoQuantum AI Just Made a Godlike Discovery!
2 months agoLatest Videos
Stop Buying Apartments Blindly: 3 Hidden...
Jul 08, 2026Super Girl Movie 2026 is finally here!
Jul 05, 2026Ethiopian Kitfo
Jul 05, 2026Related Articles
The Future of AI in the IT Industry: Tra...
1 year ago
5G: What It Means for You
2 years ago
