How can one simplify the validation of phone numbers in PHP by focusing on valid characters and removing unnecessary formatting?

Validating phone numbers in PHP can be simplified by focusing on valid characters and removing unnecessary formatting. One approach is to strip all non-numeric characters from the phone number and then check if the resulting string contains only digits. This way, we can ensure that the phone number consists of valid characters without being concerned about specific formatting.

function validatePhoneNumber($phoneNumber) {
    $phoneNumber = preg_replace('/\D/', '', $phoneNumber); // Remove all non-numeric characters
    if (ctype_digit($phoneNumber)) {
        return true; // Phone number contains only digits
    } else {
        return false; // Phone number contains invalid characters
    }
}

// Example usage
$phoneNumber = "+1 (555) 123-4567";
if (validatePhoneNumber($phoneNumber)) {
    echo "Phone number is valid.";
} else {
    echo "Phone number is invalid.";
}