Adding spaces at specific positions in a numeric string

numbers, php, preg-replace, regex, spaces

Solution

For your concrete example this would do it:

$numbers = '90000800123987';
$regex = '/(\\d{3})(\\d{2})(\\d{3})(\\d{3})(\\d{3})/';
$result = preg_replace($regex, '$1 $2 $3 $4 $5', $numbers);

Problem

I would like to add spaces between set of numbers with `preg_replace()`; `$numbers = '90000800123987';` Desired output: `900 00 800 123 987` I'm quite confused at this point, I couldn't get the desired output so far.

Original source