Limit Text in Laravel
Limit text in Laravel can be done using the Str helper class from the Illuminate\Support\Str namespace. The Str::limit method truncates the given string to the specified length and the Str::words method limits the number of words in a string. The Str helper class provides many functions other than the limit
for working with string.
We can also limit the number of characters in a string using CSS and JavaScript in the front end.
In this article we are going to discuss:
- Limit string length by character count
- Limit string length by words count
Limit string length by character count
The Str::limit method truncates the given string to the given length by its character count. We can use it at the controller level, blade template, and in the model as well.
Limit string character length in the controller
We can add laravel str limit in the controller as given below.
Import Illuminate\Support\Str
at the top.
use Illuminate\Support\Str;
Then use Str::limit()
to limit the string length as below.
Str::limit("Hello World!", 5);
Output:
Hello...
You may be noticed that three dots are appending to the above output of Str::limit()
. You may pass a third argument to the method to change the string that will be appended to the end of the truncated string.
If we want to avoid the appending text we can pass the third argument as empty like the given below code.
Str::limit("Hello World!", 5, "");
Output:
Hello
If want to append some text at the end of the truncated string, we can do as given below.
use Illuminate\Support\Str;
$truncated = Str::limit('This is a sample text content', 20, ' (...)');
// This is a sample text (...)
Limit string character length in the blade template
We can use the Str helper class in the blade template as given below.
<p>{{ Illuminate\Support\Str::limit($data, 20) }}</p>
Here, $data
is the actual string and 20 is the number of characters that you would like to limit the string.
Limit string character length in the Model
Using Str::limit() in the blade is not a best practice since we want to add limit() function in each and every place in the blade template where the text is used. So we can set the limit on the model level as given below.
...
use Illuminate\Support\Str;
class Product
{
protected $fillable = [
..., 'description'
]
public function shortDescription()
{
return Str::limit($this->description, 20 )
}
}
Instead of taking description
object we can use the truncated object as shortDescription
.
Limit string length by words count
The Str::words method limits string length by word count in a string. An additional string can be passed to this method as its third argument to specify which string to be appended at the end of the truncated string.
import Illuminate\Support\Str
at the top.
use Illuminate\Support\Str;
Then use Str::words()
to limit the string length by word count as below.
return Str::words('Laravel is a web application framework with expressive, elegant syntax.', 6, ' >>>');
Output:
Laravel is a web application framework >>>
Conclusion
Laravel makes it quite easy to string operations using the Str helper class in our blade view, controller, and even in the model.
The Str::limit method truncates the given string to the given length by its character count and the Str::words method truncates words in the string by the given word count.
To learn more about laravel helper classes, check laravel documentation.
On datainfinities.com, Read articles all around JavaScript, React, Node.js, PHP, Laravel, and Shopify.
Related Blogs
The POST method is not supported for this route. Supported methods: GET, HEAD
419 page expired error in Laravel
How to solve error: array_merge() does not accept unknown named parameters
Laravel Cron Job Task Scheduling Tutorial
Delete an element from an array in PHP
How to create multiple where clause query using Laravel eloquent
Laravel Order by Pivot Table Field
How to display validation error messages in Laravel blade view
Get raw SQL query from Laravel Query Builder
Explore All Blogs