To limit the string length in Laravel, you can make use of the Str::limit() method provided by Laravel's Illuminate\Support\Str class. This method allows you to truncate a string to a specified maximum length while appending a custom ending if necessary. Here's an example of how to limit the string length in Laravel:
use Illuminate\Support\Str;
$originalString = "This is a long string that needs to be limited.";
// Limit the string to a maximum of 20 characters with "..." appended at the end
$limitedString = Str::limit($originalString, 20, '...');
echo $limitedString;
Output
This is a long...
In the example above, the Str::limit() method is used to limit the $originalString to a maximum length of 20 characters. If the original string exceeds the limit, it will be truncated and the custom ending ("...") will be appended to the truncated string.
You can adjust the maximum length and the custom ending as per your requirements. The Str::limit() method is a convenient way to ensure that your string lengths are within the desired limits for displaying or storing purposes.
To limit the word length in Laravel
you can use the str_limit_words helper function. This function allows you to truncate a string to a specified maximum number of words. Here's an example of how to limit the word length in Laravel:
use Illuminate\Support\Str;
$originalString = "This is a long string that needs to be limited to a certain number of words.";
// Limit the string to a maximum of 5 words
$limitedString = str_limit_words($originalString, 5, '');
echo $limitedString;
OUTPUT
This is a long string...
In the example above, the str_limit_words() function is used to limit the $originalString to a maximum of 5 words. If the original string contains more than 5 words, it will be truncated, and no custom ending will be appended.
The str_limit_words() function is a helper function provided by Laravel and can be used directly in your code. It is a convenient way to limit the word length of a string in Laravel without having to write a custom function.
Practical Code
@foreach($brokerdatas as $broker_data)
<div class="col-sm-7">
<div class="card">
<div class="card-body">
<h5 class="card-title">{{ $broker_data->item_title }}</h5>
<p class="card-text">{{ $broker_data->city_name }},{{ $broker_data->state_abbr }},{{ $broker_data->country_abbr }}</p>
<p class="card-text"> {{Str::limit($broker_data->item_description,90)}}</p>
</div>
</div>
</div>
<div class="col-sm-2">
<div class="card">
<div class="card-body">
<h5 class="card-title">{{ $broker_data->item_price }}</h5>
</div>
</div>
</div>
@endforeach
Top comments (0)