Add space after every 4th character

前端 未结 8 1877
我寻月下人不归
我寻月下人不归 2021-02-11 12:19

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

$str = $rows[\'value\'];


        
8条回答
  •  执笔经年
    2021-02-11 12:46

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

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

提交回复
热议问题