如何获取字符串中的最后 4 个字符 - Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47756385/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:05:09  来源:igfitidea点击:

How to get the last 4 characters in a string - Laravel

phplaravel

提问by Radu

I have this string in my database like 12-12-2067. I want to only display the last 4 words in the string 2067in my view. How do i achieve this ?

我的数据库中有这个字符串,比如12-12-2067. 我只想2067在我的视图中显示字符串中的最后 4 个单词。我如何实现这一目标?

$get_string = Item::all()->first();


<p>{{get_string}}</p>

回答by Radu

You can use:

您可以使用:

substr ($getstring->string, -4)

Because of (-) it will start form the end of the string and it will take the next 4 characters.

由于 (-) 它将从字符串的末尾开始,并将占用接下来的 4 个字符。

Take care that first() it will return an object and not the string you want.

注意 first() 它将返回一个对象而不是您想要的字符串。

回答by Ben

Please read the manual substr.

请阅读手册substr

$str = substr($str, -4);

回答by pardeep

string substr ( string $string , int $start [, int $length ] )

string substr ( string $string , int $start [, int $length ] )

$dynamicstring = "12-07-2017"; $newstring = substr($dynamicstring, -4);

$dynamicstring = "12-07-2017"; $newstring = substr($dynamicstring, -4);