Are there any best practices for handling and formatting phone numbers in PHP to avoid extra spaces or unwanted characters?

When handling and formatting phone numbers in PHP, it's important to remove any extra spaces or unwanted characters to ensure consistency and accuracy. One way to achieve this is by using regular expressions to clean up the phone number string before processing it further.

// Remove extra spaces and unwanted characters from a phone number
$phone_number = "+1 (555) 123-4567";
$cleaned_phone_number = preg_replace('/[^0-9]/', '', $phone_number);

echo $cleaned_phone_number; // Output: 15551234567