php Laravel 字符串降低
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32957568/
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
Laravel String To Lower
提问by locnguyen
I'm trying to convert a string to lowercase in a views page (index.blade.php)
我正在尝试在视图页面 (index.blade.php) 中将字符串转换为小写
The following is what I would like to achieve.
以下是我想要实现的目标。
<img src="images/teamnamesml.jpg logo">
This is my attempt
这是我的尝试
<img src="images/{{ Str::lower($matchup->visitorTeam) }}sml.jpg">
I get this error
我收到这个错误
FatalErrorException in ed1bb29e73e623d0f837c841ed066275 line 71:
Class 'Str' not found
Do I have to import the class Illuminate\Support\Str
to a specific file?
我是否必须将类导入Illuminate\Support\Str
到特定文件?
回答by BrokenBinary
Why not just use the PHP built-in strtolower
?
为什么不直接使用 PHP 内置的strtolower
?
<img src="images/{{ strtolower($matchup->visitorTeam) }}sml.jpg">
Or, if you need full UTF-8 support you can use mb_strtolower($string, 'UTF-8')
which allows umlauts and other fun UTF-8 stuff. This is what Laravel's Str::lower()
function does.
或者,如果您需要完整的 UTF-8 支持,您可以使用mb_strtolower($string, 'UTF-8')
它允许变音和其他有趣的 UTF-8 内容。这就是 Laravel 的Str::lower()
功能所做的。
回答by Zeussi
Because in the comments you asked still, how it works in the Laravel way, so here an alternative solution next to strtolower
and mb_strtolower
, which also work fine.
因为在你问的评论中,它是如何以 Laravel 方式工作的,所以这里有一个strtolower
和旁边的替代解决方案mb_strtolower
,它也可以正常工作。
You have to add the namsepace in front of the method, that PHP and Laravel can find the method.
您必须在方法前添加 namsepace,以便 PHP 和 Laravel 可以找到该方法。
So, if you want to use it in Blade, do the following:
因此,如果您想在 Blade 中使用它,请执行以下操作:
<img src="images/{{ Illuminate\Support\Str::lower($matchup->visitorTeam) }}sml.jpg">
If you want to use it in a Controller or Model, you have to add the namespace, where Str
is in on the top:
如果要在控制器或模型中使用它,则必须添加命名空间,其中Str
位于顶部:
use Illuminate\Support\Str;
After that, you can call it without the namespace prefix:
之后,您可以在没有命名空间前缀的情况下调用它:
Str::lower($test);
回答by GTCrais
Consider using mb_strtolower
to be able to convert any character that has 'alphabetic' property, such as ?, ?
etc.
考虑使用mb_strtolower
能够转换任何具有“字母”属性的字符,例如?, ?
等。