What are the potential pitfalls of using the preg_match() function in PHP for phone number validation?

One potential pitfall of using the preg_match() function for phone number validation is that it may not cover all possible phone number formats or variations. To solve this issue, it is recommended to use a more robust validation method, such as a dedicated phone number validation library or service.

// Using a dedicated phone number validation library
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$phoneNumber = $phoneUtil->parse($inputPhoneNumber, $inputRegionCode);

if ($phoneUtil->isValidNumber($phoneNumber)) {
    // Phone number is valid
} else {
    // Phone number is invalid
}