Truncate string in Laravel blade templates

后端 未结 11 1064
死守一世寂寞
死守一世寂寞 2020-12-07 15:28

Is there a truncate modifier for the blade templates in Laravel, pretty much like Smarty?

I know I could just write out the actual php in the template but i\'m looki

11条回答
  •  囚心锁ツ
    2020-12-07 16:05

    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.

    //For Laravel 4 to Laravel 5.5
    {{ str_limit($string, $limit = 150, $end = '...') }}
    //For Laravel 5.5 upwards
    {{ \Illuminate\Support\Str::limit($string, 150, $end='...') }}
    

    For more Laravel helper functions http://laravel.com/docs/helpers#strings

提交回复
热议问题