Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Limit Characters - PHP [duplicate]

I used this line of code to show next post title on my app

<span class="blog-title-next"> {{ $post->nextPost()['title'] }} </span>

How can I limit the title name to show only first 10 characters?

Thanks.

like image 951
Nikola Avatar asked Dec 16 '15 18:12

Nikola


People also ask

How do I limit characters in laravel?

Just change the $your_string part with your actual string and also the 50 part with the number of characters that you would like to limit your string to.

How do I trim string in laravel blade?

In Laravel 4 & 5 (up to 5.7), you can use str_limit, which limits the number of characters in a string. While in Laravel 5.8 up, you can use the Str::limit helper.

How do you find the length of a string in laravel?

You can use laravel helper method Str::length() to find string length or you can use php strlen() method .


1 Answers

You could use the str_limit() helper function:

{{ str_limit($post->nextPost()['title'], 10) }}

For newer version of laravel

use Illuminate\Support\Str;

$truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20, ' (...)');

// The quick brown fox (...)
like image 111
Pantelis Peslis Avatar answered Sep 20 '22 01:09

Pantelis Peslis