limit text using str_limit function in Laravel 5.5

﹥>﹥吖頭↗ 提交于 2020-08-07 20:00:36

问题


I have been trying to limit my blog content text with str_limit that works fine until I apply limit of characters on it. kindly see what is missing in the code of my blade file:

{!! str_limit($blog->content) !!} works fine with the default limit, showing limited text on the view. But when I apply any custom limit i.e. {!! str_limit($blog->content, 20) !!} it do not show any text on the view.


回答1:


The str_limit function has been deprecated, but you can use Str::limit($text)Laravel doc.

In the php code use:

use \Illuminate\Support\Str;

The sample usage in blade:

{{\Illuminate\Support\Str::limit($text)}}
{{\Illuminate\Support\Str::limit($text,10)}}



回答2:


Have sorted out this thing using {!! substr(strip_tags($blog->content), 0, 150) !!} , works fine with what I was requiring.




回答3:


In laravel 7, all you have to do is open your blog directory in terminal and install "composer require laravel/helpers" without quotes.. after that, close your artisan and run php artisan serve again. go back to ur file and now you can use str_limit()



来源:https://stackoverflow.com/questions/51095567/limit-text-using-str-limit-function-in-laravel-5-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!