Php 用Laravel显示博客文章摘录

Php 用Laravel显示博客文章摘录,php,laravel,Php,Laravel,我试图展示博客文章的摘录。我可以用php和mysql实现这一点。然而,我想知道在Laravel框架内是否有一种更“友好”的方法来实现这一点 谢谢 您可以使用方法words 默认值为: words($value,$words = 100, $end='...'); 您可以这样实现它 Str::words($post->body); Str::words($post->body,10); //will show first 10 words 如果出现任何错误,如找不到Str类,请

我试图展示博客文章的
摘录。我可以用
php
mysql
实现这一点。然而,我想知道在Laravel框架内是否有一种更“友好”的方法来实现这一点


谢谢

您可以使用方法
words

默认值为:

 words($value,$words = 100, $end='...');
您可以这样实现它

Str::words($post->body);

Str::words($post->body,10); //will show first 10 words
如果出现任何错误,如找不到Str类,请使用以下语句

use Illuminate\Support\Str;

您可以轻松使用
stru limit

在PHP文件中:

str_limit($value, $limit = 100, $end = '...')
在文件中:

我看到这是从版本到


很抱歉我迟到了,但希望它能帮助某人:

这样使用它可能会引发“类'Str'未找到错误”:

与完整名称空间一起使用,如下所示:

\Illuminate\Support\Str::words($post->body, 10);
\Illuminate\Support\Str::words($post->body, 10,'...');
str_limit($post->body, 10);
或带点:

\Illuminate\Support\Str::words($post->body, 10);
\Illuminate\Support\Str::words($post->body, 10,'...');
str_limit($post->body, 10);
或者在config/app.php中将函数注册为Facade,尝试按如下方式添加:

'aliases' => [
...
        'Str' => Illuminate\Support\Str::class,
...
]
然后使用Str::anywhere!,因此,您可以自由使用: 单词($post->body,10)

需要简洁明了的解决方案,只需使用Laravel的助手功能:

\Illuminate\Support\Str::words($post->body, 10);
\Illuminate\Support\Str::words($post->body, 10,'...');
str_limit($post->body, 10);

str_limit($post->body, 10,'...');

上面提到的Str类的一些答案从laravel core中删除,并转移到新的helpers包中。如《LaravelV6.x升级指南》中所述。

“所有
str_
array_
助手都已移动到新的
laravel/helpers
Composer包中,并从框架中删除。如果需要,您可以更新对这些助手的所有呼叫,以使用
illumb\Support\Str
illumb\Support\Arr
类。或者,您可以将新的laravel/helpers包添加到应用程序中,以继续使用这些帮助程序:


只需通过Composer安装它,它就可以正常工作。

您可以将
\illumb\Support\Str
类与
Str::limit($string,$limit,$end)
一起使用,例如:

use \Illuminate\Support\Str;
/*...*/
Str::limit($myString, 50, '>>>');

你的PHP代码是什么?非常感谢你的帮助。欢迎@ViperTecPro