diff --git a/src/PhoneNumberUtil.php b/src/PhoneNumberUtil.php index d8880db0..007794e4 100644 --- a/src/PhoneNumberUtil.php +++ b/src/PhoneNumberUtil.php @@ -2577,6 +2577,9 @@ public function formatOutOfCountryKeepingAlphaChars(PhoneNumber $number, string $regionCode = $this->getRegionCodeForCountryCode($countryCode); // Metadata cannot be null because the country calling code is valid. $metadataForRegion = $this->getMetadataForRegionOrCallingCode($countryCode, $regionCode); + // Strip any extension + $this->maybeStripExtension($formattedNumber); + // Append the formatted extension $this->maybeAppendFormattedExtension( $number, $metadataForRegion, diff --git a/tests/core/PhoneNumberUtilTest.php b/tests/core/PhoneNumberUtilTest.php index d31e5655..1dab19fc 100644 --- a/tests/core/PhoneNumberUtilTest.php +++ b/tests/core/PhoneNumberUtilTest.php @@ -875,6 +875,13 @@ public function testFormatOutOfCountryKeepingAlphaChars(): void $this->phoneUtil->formatOutOfCountryKeepingAlphaChars($alphaNumericNumber, RegionCode::BS) ); + // Testing a number with extension. + $alphaNumericNumberWithExtn = $this->phoneUtil->parseAndKeepRawInput('800 SIX-flag ext. 1234', RegionCode::US); + $this->assertEquals( + '0011 1 800 SIX-FLAG extn. 1234', + $this->phoneUtil->formatOutOfCountryKeepingAlphaChars($alphaNumericNumberWithExtn, RegionCode::AU) + ); + // Testing that if the raw input doesn't exist, it is formatted using // formatOutOfCountryCallingNumber. $alphaNumericNumber->clearRawInput();