How can special characters and spaces be removed from a telephone number in PHP?

Special characters and spaces can be removed from a telephone number in PHP by using the str_replace() function to replace any non-numeric characters with an empty string. This will leave only the numeric digits in the telephone number.

$telephone_number = "+1 (555) 123-4567";
$cleaned_number = str_replace([' ', '(', ')', '-', '+'], '', $telephone_number);
echo $cleaned_number; // Output: 15551234567