3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function format_phone($number) { $num = preg_replace('/[^0-9]/', '', $number); $l = strlen($num); if ($l < 10) { return false; } preg_match('/^([0-9]{3})([0-9]{3})([0-9]{4})([0-9]*)$/', $num, $parts); array_shift($parts); $format = '%1$d-%2$d-%3$d'; if ($l > 10) { $format .= ' x%4$d'; } return sprintf($format, ... $parts); } var_dump(format_phone('8045551234'));
based on KZ8UM
Output for 5.6.0 - 5.6.30, hhvm-3.15.4, 7.0.0 - 7.1.7
string(12) "804-555-1234"