libphonenumber\PhoneNumberUtil::maybeStripNationalPrefixAndCarrierCode PHP Method

maybeStripNationalPrefixAndCarrierCode() public method

Strips any national prefix (such as 0, 1) present in the number provided.
public maybeStripNationalPrefixAndCarrierCode ( string &$number, PhoneMetadata $metadata, string &$carrierCode ) : boolean
$number string the normalized telephone number that we wish to strip any national dialing prefix from
$metadata PhoneMetadata the metadata for the region that we think this number is from
$carrierCode string a place to insert the carrier code if one is extracted
return boolean true if a national prefix or carrier code (or both) could be extracted.
    public function maybeStripNationalPrefixAndCarrierCode(&$number, PhoneMetadata $metadata, &$carrierCode)
    {
        $numberLength = mb_strlen($number);
        $possibleNationalPrefix = $metadata->getNationalPrefixForParsing();
        if ($numberLength == 0 || $possibleNationalPrefix === null || mb_strlen($possibleNationalPrefix) == 0) {
            // Early return for numbers of zero length.
            return false;
        }
        // Attempt to parse the first digits as a national prefix.
        $prefixMatcher = new Matcher($possibleNationalPrefix, $number);
        if ($prefixMatcher->lookingAt()) {
            $nationalNumberRule = $metadata->getGeneralDesc()->getNationalNumberPattern();
            // Check if the original number is viable.
            $nationalNumberRuleMatcher = new Matcher($nationalNumberRule, $number);
            $isViableOriginalNumber = $nationalNumberRuleMatcher->matches();
            // $prefixMatcher->group($numOfGroups) === null implies nothing was captured by the capturing
            // groups in $possibleNationalPrefix; therefore, no transformation is necessary, and we just
            // remove the national prefix
            $numOfGroups = $prefixMatcher->groupCount();
            $transformRule = $metadata->getNationalPrefixTransformRule();
            if ($transformRule === null || mb_strlen($transformRule) == 0 || $prefixMatcher->group($numOfGroups - 1) === null) {
                // If the original number was viable, and the resultant number is not, we return.
                $matcher = new Matcher($nationalNumberRule, substr($number, $prefixMatcher->end()));
                if ($isViableOriginalNumber && !$matcher->matches()) {
                    return false;
                }
                if ($carrierCode !== null && $numOfGroups > 0 && $prefixMatcher->group($numOfGroups) !== null) {
                    $carrierCode .= $prefixMatcher->group(1);
                }
                $number = substr($number, $prefixMatcher->end());
                return true;
            } else {
                // Check that the resultant number is still viable. If not, return. Check this by copying
                // the string and making the transformation on the copy first.
                $transformedNumber = $number;
                $transformedNumber = substr_replace($transformedNumber, $prefixMatcher->replaceFirst($transformRule), 0, $numberLength);
                $matcher = new Matcher($nationalNumberRule, $transformedNumber);
                if ($isViableOriginalNumber && !$matcher->matches()) {
                    return false;
                }
                if ($carrierCode !== null && $numOfGroups > 1) {
                    $carrierCode .= $prefixMatcher->group(1);
                }
                $number = substr_replace($number, $transformedNumber, 0, mb_strlen($number));
                return true;
            }
        }
        return false;
    }

Usage Example

 public function testMaybeStripNationalPrefix()
 {
     $metadata = new PhoneMetadata();
     $metadata->setNationalPrefixForParsing("34");
     $phoneNumberDesc = new PhoneNumberDesc();
     $phoneNumberDesc->setNationalNumberPattern("\\d{4,8}");
     $metadata->setGeneralDesc($phoneNumberDesc);
     $numberToStrip = "34356778";
     $strippedNumber = "356778";
     $carrierCode = null;
     $this->assertTrue($this->phoneUtil->maybeStripNationalPrefixAndCarrierCode($numberToStrip, $metadata, $carrierCode));
     $this->assertEquals($strippedNumber, $numberToStrip, "Should have had national prefix stripped.");
     // Retry stripping - now the number should not start with the national prefix, so no more
     // stripping should occur.
     $carrierCode = null;
     $this->assertFalse($this->phoneUtil->maybeStripNationalPrefixAndCarrierCode($numberToStrip, $metadata, $carrierCode));
     $this->assertEquals($strippedNumber, $numberToStrip, "Should have had no change - no national prefix present.");
     // Some countries have no national prefix. Repeat test with none specified.
     $metadata->setNationalPrefixForParsing("");
     $carrierCode = null;
     $this->assertFalse($this->phoneUtil->maybeStripNationalPrefixAndCarrierCode($numberToStrip, $metadata, $carrierCode));
     $this->assertEquals($strippedNumber, $numberToStrip, "Should not strip anything with empty national prefix.");
     // If the resultant number doesn't match the national rule, it shouldn't be stripped.
     $metadata->setNationalPrefixForParsing("3");
     $numberToStrip = "3123";
     $strippedNumber = "3123";
     $carrierCode = null;
     $this->assertFalse($this->phoneUtil->maybeStripNationalPrefixAndCarrierCode($numberToStrip, $metadata, $carrierCode));
     $this->assertEquals($strippedNumber, $numberToStrip, "Should have had no change - after stripping, it wouldn't have matched the national rule.");
     // Test extracting carrier selection code.
     $metadata->setNationalPrefixForParsing("0(81)?");
     $numberToStrip = "08122123456";
     $strippedNumber = "22123456";
     $carrierCode = "";
     $this->assertTrue($this->phoneUtil->maybeStripNationalPrefixAndCarrierCode($numberToStrip, $metadata, $carrierCode));
     $this->assertEquals("81", $carrierCode);
     $this->assertEquals($strippedNumber, $numberToStrip, "Should have had national prefix and carrier code stripped.");
     // If there was a transform rule, check it was applied.
     $metadata->setNationalPrefixTransformRule("5\${1}5");
     // Note that a capturing group is present here.
     $metadata->setNationalPrefixForParsing("0(\\d{2})");
     $numberToStrip = "031123";
     $transformedNumber = "5315123";
     $carrierCode = null;
     $this->assertTrue($this->phoneUtil->maybeStripNationalPrefixAndCarrierCode($numberToStrip, $metadata, $carrierCode));
     $this->assertEquals($transformedNumber, $numberToStrip, "Should transform the 031 to a 5315.");
 }
PhoneNumberUtil