我有这个字符串
1010081-COP-8-27-20110616214459
我需要计算从该字符串末尾开始的最后 6 个字符(因为从开头开始可能会很长)
然后我需要在每 2 个字符后添加冒号。
所以从末尾算起 6 个字符后,它将是
214459
添加冒号后,它将如下所示:
21:44:59
你能帮助我实现它吗?
我真的不知道从哪里开始!
谢谢
你可以这样做substr https://www.php.net/manual/en/function.substr.php, str_split http://us.php.net/manual/en/function.str-split.php and implode https://www.php.net/manual/en/function.implode.php
为了清楚起见,该代码在多行上完成,但可以轻松地在一行上以链形式完成:
$str = '1010081-COP-8-27-20110616214459';
//Get last 6 chars
$end = substr($str, -6);
//Split string into an array. Each element is 2 chars
$chunks = str_split($end, 2);
//Convert array to string. Each element separated by the given separator.
$result = implode(':', $chunks);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)