带有短划线和数字的PHP preg_replace电话号码

I have a couple of phonenumbers listed in my database with a certain way of writing.
I want to have a space after every two numbers. I thought I would do:

$numbers = '06-12345678';
$regex = '/(\\d{2})(\\d{1})(\\d{2})(\\d{2})(\\d{2})(\\d{2})/';
$result = preg_replace($regex, '$1 $2 $3 $4 $5 $6', $numbers);
echo $result;

But this doesn't work. It just places all the numbers next to each other.

My Expected Output:

06 - 12 34 56 78

This should work for you:

$numbers = "06-12345678";
echo $result = preg_replace("/(\d{2})-(\d{2})(\d{2})(\d{2})(\d{2})/", "$1 - $2 $3 $4 $5", $numbers);

regex explanation:

(\d{2})-(\d{2})(\d{2})(\d{2})(\d{2})
  • 1st Capturing group (\d{2})
  • \d{2} match a digit [0-9]
    • Quantifier: {2} Exactly 2 times
  • - matches the character - literally
  • 2nd Capturing group (\d{2}) //<- 4x the same
  • \d{2} match a digit [0-9]
    • Quantifier: {2} Exactly 2 times

output:

06 - 12 34 56 78