Ad

Laravel Limit Characters - PHP

- 1 answer

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.

Ad

Answer

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 (...)
Ad
source: stackoverflow.com
Ad