libphonenumber\PhoneNumberUtil::maybeStripInternationalPrefixAndNormalize PHP Method

maybeStripInternationalPrefixAndNormalize() public method

Strips any international prefix (such as +, 00, 011) present in the number provided, normalizes the resulting number, and indicates if an international prefix was present.
public maybeStripInternationalPrefixAndNormalize ( string &$number, string $possibleIddPrefix ) : integer
$number string the non-normalized telephone number that we wish to strip any international dialing prefix from.
$possibleIddPrefix string string the international direct dialing prefix from the region we think this number may be dialed in
return integer the corresponding CountryCodeSource if an international dialing prefix could be removed from the number, otherwise CountryCodeSource.FROM_DEFAULT_COUNTRY if the number did not seem to be in international format.
    public function maybeStripInternationalPrefixAndNormalize(&$number, $possibleIddPrefix)
    {
        if (mb_strlen($number) == 0) {
            return CountryCodeSource::FROM_DEFAULT_COUNTRY;
        }
        $matches = array();
        // Check to see if the number begins with one or more plus signs.
        $match = preg_match('/^' . static::$PLUS_CHARS_PATTERN . '/' . static::REGEX_FLAGS, $number, $matches, PREG_OFFSET_CAPTURE);
        if ($match > 0) {
            $number = mb_substr($number, $matches[0][1] + mb_strlen($matches[0][0]));
            // Can now normalize the rest of the number since we've consumed the "+" sign at the start.
            $number = static::normalize($number);
            return CountryCodeSource::FROM_NUMBER_WITH_PLUS_SIGN;
        }
        // Attempt to parse the first digits as an international prefix.
        $iddPattern = $possibleIddPrefix;
        $number = static::normalize($number);
        return $this->parsePrefixAsIdd($iddPattern, $number) ? CountryCodeSource::FROM_NUMBER_WITH_IDD : CountryCodeSource::FROM_DEFAULT_COUNTRY;
    }

Usage Example

 public function testMaybeStripInternationalPrefix()
 {
     $internationalPrefix = "00[39]";
     $numberToStrip = "0034567700-3898003";
     // Note the dash is removed as part of the normalization.
     $strippedNumber = "45677003898003";
     $this->assertEquals(CountryCodeSource::FROM_NUMBER_WITH_IDD, $this->phoneUtil->maybeStripInternationalPrefixAndNormalize($numberToStrip, $internationalPrefix));
     $this->assertEquals($strippedNumber, $numberToStrip, "The number supplied was not stripped of its international prefix.");
     // Now the number no longer starts with an IDD prefix, so it should now report
     // FROM_DEFAULT_COUNTRY.
     $this->assertEquals(CountryCodeSource::FROM_DEFAULT_COUNTRY, $this->phoneUtil->maybeStripInternationalPrefixAndNormalize($numberToStrip, $internationalPrefix));
     $numberToStrip = "00945677003898003";
     $this->assertEquals(CountryCodeSource::FROM_NUMBER_WITH_IDD, $this->phoneUtil->maybeStripInternationalPrefixAndNormalize($numberToStrip, $internationalPrefix));
     $this->assertEquals($strippedNumber, $numberToStrip, "The number supplied was not stripped of its international prefix.");
     // Test it works when the international prefix is broken up by spaces.
     $numberToStrip = "00 9 45677003898003";
     $this->assertEquals(CountryCodeSource::FROM_NUMBER_WITH_IDD, $this->phoneUtil->maybeStripInternationalPrefixAndNormalize($numberToStrip, $internationalPrefix));
     $this->assertEquals($strippedNumber, $numberToStrip, "The number supplied was not stripped of its international prefix.");
     // Now the number no longer starts with an IDD prefix, so it should now report
     // FROM_DEFAULT_COUNTRY.
     $this->assertEquals(CountryCodeSource::FROM_DEFAULT_COUNTRY, $this->phoneUtil->maybeStripInternationalPrefixAndNormalize($numberToStrip, $internationalPrefix));
     // Test the + symbol is also recognised and stripped.
     $numberToStrip = "+45677003898003";
     $strippedNumber = "45677003898003";
     $this->assertEquals(CountryCodeSource::FROM_NUMBER_WITH_PLUS_SIGN, $this->phoneUtil->maybeStripInternationalPrefixAndNormalize($numberToStrip, $internationalPrefix));
     $this->assertEquals($strippedNumber, $numberToStrip, "The number supplied was not stripped of the plus symbol.");
     // If the number afterwards is a zero, we should not strip this - no country calling code begins
     // with 0.
     $numberToStrip = "0090112-3123";
     $strippedNumber = "00901123123";
     $this->assertEquals(CountryCodeSource::FROM_DEFAULT_COUNTRY, $this->phoneUtil->maybeStripInternationalPrefixAndNormalize($numberToStrip, $internationalPrefix));
     $this->assertEquals($strippedNumber, $numberToStrip, "The number supplied had a 0 after the match so shouldn't be stripped.");
     // Here the 0 is separated by a space from the IDD.
     $numberToStrip = "009 0-112-3123";
     $this->assertEquals(CountryCodeSource::FROM_DEFAULT_COUNTRY, $this->phoneUtil->maybeStripInternationalPrefixAndNormalize($numberToStrip, $internationalPrefix));
 }
PhoneNumberUtil