PHP计数,每2个字符添加冒号
我有这个字符串,
1010081-COP-8-27-20110616214459
我需要计算从该字符串末尾开始的最后 6 个字符(因为从头开始可能很长)
然后我需要在每 2 个字符后添加冒号。
因此,从末尾算起 6 个字符后,它会是
214459
添加冒号后,它看起来像:
21:44:59
你能帮我实现它吗?
我真的不知道从哪里开始!
谢谢
I have this string
1010081-COP-8-27-20110616214459
I need to count the last 6 characters starting from the end of this string (because it could may be long starting from the begin)
Then I need to add colons after every 2 characters.
So after counting 6 characters from the end it will be
214459
After having added the colons it will look like:
21:44:59
Can you help me achieving it?
I do not really know where to start!
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
substr
来完成此操作,str_split
和implode
代码分多行完成清晰,但可以轻松地在一行中完成:
You can do this with
substr
,str_split
andimplode
The code is done on multiple lines for clarity, but can easily be done in a chain on one line:
在我看来,该字符串具有特定的格式,您应该将其解析为数据。像这样的东西:
It looks to me though like that string has a particular format which you should parse into data. Something like:
如果你只想要时间:
If you just want the time: