= 7 && preg_match($phone_number_validation_regex, $phone) == 1; } /** * Extract dial code and phone from a full phone number like +34628288272 * * @param string $fullPhone Full phone number with country prefix (e.g. +34628288272) * @return array|null ['dial_code' => '+34', 'dial_code_country' => 'ES', 'phone' => '628288272'] or null */ function alexr_extract_dial_code_and_phone($fullPhone) { $digits = preg_replace('/[^0-9]/', '', ltrim($fullPhone, '+')); if (empty($digits)) return null; // Get all prefixes sorted by length descending (match longest first) $prefixList = \Alexr\Enums\Countries::phonePrefixList(); $prefixes = array_unique(array_values($prefixList)); usort($prefixes, function($a, $b) { return strlen($b) - strlen($a); }); foreach ($prefixes as $prefix) { if (strpos($digits, $prefix) === 0) { $phone = substr($digits, strlen($prefix)); if (strlen($phone) >= 6) { $country = \Alexr\Enums\Countries::countryByPrefix($prefix); return [ 'dial_code' => '+' . $prefix, 'dial_code_country' => $country ?? '', 'phone' => $phone, ]; } } } return null; }