php 在 Laravel 刀片模板中截断字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15012712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Truncate string in Laravel blade templates
提问by fl3x7
Is there a truncate modifier for the blade templates in Laravel, pretty much like Smarty?
Laravel 中的刀片模板是否有截断修饰符,就像 Smarty 一样?
I know I could just write out the actual php in the template but i'm looking for something a little nicer to write (let's not get into the whole PHP is a templating engine debate).
我知道我可以在模板中写出实际的 php,但我正在寻找更好的东西来写(让我们不要进入整个 PHP 是模板引擎辩论)。
So for example i'm looking for something like:
例如,我正在寻找类似的东西:
{{ $myVariable|truncate:"10":"..." }}
I know I could use something like Twig via composer but I'm hoping for built in functionality in Laravel itself.
我知道我可以通过 Composer 使用像 Twig 这样的东西,但我希望 Laravel 本身具有内置功能。
If not is it possible to create your own reusable modifiers like Smarty provides. I like the fact that Blade doesn't overkill with all the syntax but I think truncate is a real handy function to have.
如果没有,可以像 Smarty 提供的那样创建自己的可重用修饰符。我喜欢 Blade 不会对所有语法过度使用这一事实,但我认为 truncate 是一个真正方便的功能。
I'm using Laravel 4.
我正在使用 Laravel 4。
回答by Anil Singh
In Laravel 4 & 5 (up to 5.7), you can use str_limit, which limits the number of characters in a string.
在 Laravel 4 & 5(最高 5.7)中,您可以使用str_limit,它限制了字符串中的字符数。
While in Laravel 5.8 up, you can use the Str::limithelper.
在 Laravel 5.8 up 中,你可以使用Str::limithelper。
//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
更多 Laravel 辅助函数http://laravel.com/docs/helpers#strings
回答by Dustin Graham
Laravel 4 has Str::limitwhich will truncate to the exact number of characters, and also Str::wordswhich will truncate on word boundary.
Laravel 4Str::limit将截断到确切的字符数,并且Str::words将截断单词边界。
Check out:
查看:
回答by Laurence
Edit: This answer is was posted during the Laravel 4 beta, when Str class did not exist. There is now a better way to do it in Laravel 4 - which is Dustin's answer below. I cannot delete this answer due to SO rules (it wont let me)
编辑:这个答案是在 Laravel 4 beta 期间发布的,当时 Str 类不存在。现在在 Laravel 4 中有更好的方法来做到这一点 - 这是下面达斯汀的回答。由于 SO 规则,我无法删除此答案(它不会让我)
Blade itself does not have that functionality.
Blade 本身没有这个功能。
In Laravel 3 there was the Str class - which you could do:
在 Laravel 3 中有 Str 类 - 你可以这样做:
{{ Str::limit($myVariable, 10) }}
At this stage I do not believe the Str class is in Laravel 4 - but here is a port of it that you can include in composerto add to your own project
在这个阶段,我不相信 Str 类在 Laravel 4 中 -但这里是它的一个端口,您可以将其包含在 composer 中以添加到您自己的项目中
回答by Vipertecpro
Update for Laravel 7.*: Fluent Stringsi.e a more fluent, object-oriented interface for working with string values, allowing you to chain multiple string operations together using a more readable syntax compared to traditional string operations.
Laravel 7.* 更新:Fluent Strings,即一个更流畅、面向对象的接口,用于处理字符串值,与传统字符串操作相比,允许您使用更易读的语法将多个字符串操作链接在一起。
limitExample :
限制示例:
$truncated = Str::of('The quick brown fox jumps over the lazy dog')->limit(20);
Output
输出
The quick brown fox...
wordsExample :
单词示例:
$string = Str::of('Perfectly balanced, as all things should be.')->words(3, ' >>>');
Output
输出
Perfectly balanced, as >>>
Update for Laravel 6.*: You require this package to work all laravel helpers
composer require laravel/helpers
Laravel 6.* 更新:您需要此包才能运行所有 Laravel 助手
composer require laravel/helpers
For using helper in controller, don't forget to include/use class as well
要在控制器中使用 helper,也不要忘记包含/使用类
use Illuminate\Support\Str;
Laravel 5.8 Update
Laravel 5.8 更新
This is for handling characters from the string:
这是用于处理字符串中的字符:
{!! Str::limit('Lorem ipsum dolor', 10, ' ...') !!}
Output
输出
Lorem ipsu ...
This is for handling words from the string:
这是用于处理字符串中的单词:
{!! Str::words('Lorem ipsum dolor', 2, ' ...') !!}
Output
输出
Lorem ipsum ...
Here is the latest helper documentation for handling string Laravel Helpers
这是处理字符串Laravel Helpers的最新帮助文档
回答by Raman Bhasker
You can Set the namespace like:
您可以像这样设置命名空间:
{!! \Illuminate\Support\Str::words($item->description, 10,'....') !!}
回答by Guillaume Pommier
To keep your code DRY, and if your content comes from your model you should adopt a slightly different approach. Edit your model like so (tested in L5.8):
为了保持你的代码干燥,如果你的内容来自你的模型,你应该采用稍微不同的方法。像这样编辑模型(在 L5.8 中测试):
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class Comment extends Model
{
public function getShortDescriptionAttribute()
{
return Str::words($this->description, 10, '...');
}
}
?>
Then in your view :
那么在你看来:
{{ $comment->short_description }}
回答by Ganesh Khadka
回答by Ofor Emma
This works on Laravel 5:
这适用于 Laravel 5:
{!!strlen($post->content) > 200 ? substr($post->content,0,200) : $post->content!!}
回答by stefanr
For simple things like this I would prefer to make a helper - for example:
对于像这样的简单事情,我更愿意做一个助手 - 例如:
create a helpers.phpfile in your /app/helpers.phpwith following content:
helpers.php在您的文件中创建一个/app/helpers.php包含以下内容的文件:
<?php
if (! function_exists('short_string')) {
function short_string($str) {
$rest = substr($str, 0, 10);
return $rest;
}
}
Register the helper.phpat autoload in your composer.json
helper.php在自动加载中注册composer.json
"autoload": {
"files": [
"app/helpers.php"
],
"psr-4": {
"App\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
]
}
After that you can use in your blade file for example:
之后,您可以在刀片文件中使用,例如:
{{ short_string($whatever_as_text) }}
You can use this simple function, then, globally in your app.
然后,您可以在您的应用程序中全局使用这个简单的函数。
回答by Shamsheer
Laravel 6 Update:
Laravel 6 更新:
@php
$value = 'Artificial Intelligence';
$var = Str::limit($value, $limit = 15, $end = '');
print_r($var);
@endphp
<p class="card-text">{{ Illuminate\Support\Str::limit($value, 7) }}</p>
<h2 class="main-head">{!! Str::limit($value, 5) !!}</h2>


