Substr().每n位数字添加一个字符 [英] Substr(). Add characters for each n digits
本文介绍了Substr().每n位数字添加一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的琴弦长度可能不同
$k= "2013-01-0112:00:002013-01-0212:00:002013-01-0312:00:00";
我正在寻找substr(),str_replace()或任何其他函数给我
I'm looking for substr(), str_replace() or any other function my return me this
$newstring= ('2013-01-01 12:00:00'), ('2013-01-02 12:00:00'), ('2013-01-02 12:00:00')
我应该对每18个字符使用一种,因为原始字符串的日期可能会有所不同.
I should use a kind of for each 18 charcters as the dates of the original string may vary.
我的意图是使用日期"列中的所有生成日期来构建"INSERT INTO" MYSQL查询.
My intent is to build then a "INSERT INTO" MYSQL query with all the generated dates in the column "date".
id date
null 2013-01-01 12:00:00
null 2013-01-02 12:00:00
null 2013-01-03 12:00:00
推荐答案
在第18个位置分割字符串.然后在数组第10个位置的每个值上添加空格.
Split string on every 18th position. Then add space on every value of the array on 10th position.
$k = "2013-01-0112:00:002013-01-0212:00:002013-01-0312:00:00";
$a = str_split($k, 18);
$a = array_map(function($dt) {
return implode(' ', str_split($dt, 10));
}, $a);
print_r($a);
演示 .
Demo.
如果要将字符添加到第N个位置:
If you wish to add character to N-th position:
$k = "2013-01-0112:00:002013-01-0212:00:002013-01-0312:00:00";
echo implode(',', str_split($k, 18));
// ^ ^
// character N-th position
演示 .
Demo.
这篇关于Substr().每n位数字添加一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文