Based on @rray's response
$separado = implode(' ', str_split('9999999999999999', 4));
If you wanted with dots:
$separado = implode('.', str_split('9999999999999999', 4));
As already explained, split
divides the string into equal parts. In addition, implode
"glue" the separated pieces, using a string of your choice between them.
If an extra space at the end is not a problem, see the more elegant alternative with chunk_split
in the response mentioned.
If the number of digits varies and you want something like
99 9999 9999
You can use this alternative:
for($i=strlen($string); $i>0; $i-=4) $string=substr_replace($string, ' ', $i, 0);
It inserts spaces "from the end to the beginning". See working at IDEONE .