Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dsadsadsa's avatar

PHP add a space after the first character and then every four

Hello, In the string I would like to add a spacer after the first character, and then every next 3 characters. I tried something like this, but this code adds spaces every 3 characters:

chunk_split($string, 3, ' ');

0 likes
1 reply
ftiersch's avatar
$new = substr($old, 0, 1) . " " . chunk_split(substr($old, 1), 3, ' ');

Not pretty though :D

Please or to participate in this conversation.