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})
output:
06 - 12 34 56 78