多次在字符串中添加字符 [英] adding characters in a string multiple times
本文介绍了多次在字符串中添加字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将数字以空格分隔,以4为一组.1234567890,将变为1234 5678 90
I want to separate numbers by a space, in sets of 4. e.g. 1234567890, would become 1234 5678 90
我设法创建了一个脚本来实现这一目标,但似乎最重要的是,有没有更简单的方法来实现这一目标?
I managed to create a script to achieve this, but it seems over the top, is there any easier way of achieving this?
$num = 23853267362365;
$count = strlen($num)/4;
$new_num = array();
for ($x = 1; $x <= $count; $x++) {
$num_len = strlen($num);
if($num_len>4) {
$new_num[] = substr($num,0,4);
$num = substr($num,4,$num_len-4);
}
}
$num = implode(' ',$new_num);
推荐答案
chunk_split()
:
echo chunk_split(1234567890, 4, ' ');
链接: http://php.net/manual/function.chunk-split.php
这篇关于多次在字符串中添加字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文