php 每 4 个字符后添加空格

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

Add space after every 4th character

php

提问by user990767

I want to add a space to some output after every 4th character until the end of the string. I tried:

我想在每 4 个字符之后向某些输出添加一个空格,直到字符串结束。我试过:

$str = $rows['value'];
<? echo substr($str, 0, 4) . ' ' . substr($str, 4); ?>

Which just got me the space after the first 4 characters.

这让我在前 4 个字符后得到了空间。

How can I make it show after every 4th ?

我怎样才能让它每 4 次显示一次?

回答by Felix Kling

You can use chunk_split[docs]:

您可以使用chunk_split[文档]

$str = chunk_split($rows['value'], 4, ' ');

DEMO

演示

If the length of the string is a multiple of four but you don't want a trailing space, you can pass the result to trim.

如果字符串的长度是 4 的倍数但您不想要尾随空格,则可以将结果传递给trim.

回答by Olemak

Wordwrap does exactly what you want:

Wordwrap 完全符合您的要求:

echo wordwrap('12345678' , 4 , ' ' , true )

will output: 1234 5678

将输出:1234 5678

If you want, say, a hyphen after every second digit instead, swap the "4" for a "2", and the space for a hyphen:

如果你想在每第二个数字后面加一个连字符,把“4”换成“2”,把空格换成一个连字符:

echo wordwrap('1234567890' , 2 , '-' , true )

will output: 12-34-56-78-90

将输出:12-34-56-78-90

Reference - wordwrap

参考 - 自动换行

回答by Sgarz

Have you already seen this function called wordwrap? http://us2.php.net/manual/en/function.wordwrap.php

你见过这个叫做 wordwrap 的函数吗? http://us2.php.net/manual/en/function.wordwrap.php

Here is a solution. Works right out of the box like this.

这是一个解决方案。像这样开箱即用。

<?php
$text = "Thiswordissoverylong.";
$newtext = wordwrap($text, 4, "\n", true);
echo "$newtext\n";
?>

回答by Giova

one-liner:

单线:

$yourstring = "1234567890";
echo implode(" ", str_split($yourstring, 4))." ";

This should give you as output:
1234 5678 90

这应该给你作为输出:
1234 5678 90

That's all :D

就是这样:D

回答by Meloman

Here is an example of string with length is not a multiple of 4 (or 5 in my case).

这是一个长度不是 4 的倍数(或在我的情况下为 5)的字符串示例。

function ref_format($str, $step, $reverse = false) {

    if ($reverse)
        return strrev(chunk_split(strrev($str), $step, ' '));

    return chunk_split($str, $step, ' ');
}

Use :

用 :

echo ref_format("0000000152748541695882", 5);

result: 00000 00152 74854 16958 82

结果:00000 00152 74854 16958 82

Reverse mode use ("BVR code" for swiss billing) :

反向模式使用(“BVR 代码”用于瑞士计费):

echo ref_format("1400360152748541695882", 5, true);

result: 14 00360 15274 85416 95882

结果:14 00360 15274 85416 95882

Hope it could help some of you.

希望它可以帮助你们中的一些人。

回答by hakre

On way would be to split into 4-character chunks and then join them together again with a space between each part.

方法是分成 4 个字符的块,然后将它们再次连接在一起,每个部分之间有一个空格。

As this would technically miss to insert one at the very end if the last chunk would have exactly 4 characters, we would need to add that one manually (Demo):

由于如果最后一个块正好有 4 个字符,这在技术上会错过在最后插入一个,我们需要手动添加一个(Demo):

$chunk_length = 4;
$chunks = str_split($str, $chunk_length);
$last = end($chunks);
if (strlen($last) === $chunk_length) {
    $chunks[] = '';
}
$str_with_spaces = implode(' ', $chunks);

回答by fdomig

The function wordwrap()basically does the same, however this should work as well.

该功能wordwrap()基本上执行相同的操作,但是这也应该有效。

$newstr = '';
$len = strlen($str); 
for($i = 0; $i < $len; $i++) {
    $newstr.= $str[$i];
    if (($i+1) % 4 == 0) {
        $newstr.= ' ';
    }
}

回答by Eva

PHP3 Compatible:

PHP3兼容:

Try this:

尝试这个:

$strLen = strlen( $str );
for($i = 0; $i < $strLen; $i += 4){
  echo substr($str, $i, 4) . ' ';
} 
unset( $strLen );

回答by Nitin Divate

StringBuilder str = new StringBuilder("ABCDEFGHIJKLMNOP");
int idx = str.length() - 4;
while (idx > 0){
  str.insert(idx, " ");
  idx = idx - 4;
}
return str.toString();

Explanation, this code will add space from right to left:

说明,这段代码会从右到左添加空格:

 str = "ABCDEFGH" int idx = total length - 4; //8-4=4
    while (4>0){
        str.insert(idx, " "); //this will insert space at 4th position
        idx = idx - 4; // then decrement 4-4=0 and run loop again
    }

The final output will be:

最终输出将是:

ABCD EFGH