Add space after every 4th character

前端 未结 8 1879
我寻月下人不归
我寻月下人不归 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:38

    Wordwrap does exactly what you want:

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

    will output: 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:

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

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

    Reference - wordwrap

提交回复
热议问题