What is the best approach to format a phone number in PHP with a specific pattern, considering varying lengths of the input string?

When formatting a phone number in PHP with a specific pattern, it's important to consider varying lengths of the input string. One approach is to use regular expressions to match and extract the digits from the input string, then apply the desired formatting pattern. This allows for flexibility in handling different phone number formats while ensuring consistency in the output.

$input_number = "1234567890"; // Example input phone number
$formatted_number = preg_replace("/(\d{3})(\d{3})(\d{4})/", "$1-$2-$3", preg_replace("/[^\d]/", "", $input_number));
echo $formatted_number; // Output: 123-456-7890