libphonenumber\PhoneNumberUtil::extractPossibleNumber PHP Method

extractPossibleNumber() public static method

Attempts to extract a possible number from the string passed in. This currently strips all leading characters that cannot be used to start a phone number. Characters that can be used to start a phone number are defined in the VALID_START_CHAR_PATTERN. If none of these characters are found in the number passed in, an empty string is returned. This function also attempts to strip off any alternative extensions or endings if two or more are present, such as in the case of: (530) 583-6985 x302/x2303. The second extension here makes this actually two phone numbers, (530) 583-6985 x302 and (530) 583-6985 x2303. We remove the second extension so that the first number is parsed correctly.
public static extractPossibleNumber ( integer $number ) : string
$number integer the string that might contain a phone number
return string the number, stripped of any non-phone-number prefix (such as "Tel:") or an empty string if no character used to start phone numbers (such as + or any digit) is found in the number
    public static function extractPossibleNumber($number)
    {
        if (static::$VALID_START_CHAR_PATTERN === null) {
            static::initValidStartCharPattern();
        }
        $matches = array();
        $match = preg_match('/' . static::$VALID_START_CHAR_PATTERN . '/ui', $number, $matches, PREG_OFFSET_CAPTURE);
        if ($match > 0) {
            $number = substr($number, $matches[0][1]);
            // Remove trailing non-alpha non-numerical characters.
            $trailingCharsMatcher = new Matcher(static::$UNWANTED_END_CHAR_PATTERN, $number);
            if ($trailingCharsMatcher->find() && $trailingCharsMatcher->start() > 0) {
                $number = substr($number, 0, $trailingCharsMatcher->start());
            }
            // Check for extra numbers at the end.
            $match = preg_match('%' . static::$SECOND_NUMBER_START_PATTERN . '%', $number, $matches, PREG_OFFSET_CAPTURE);
            if ($match > 0) {
                $number = substr($number, 0, $matches[0][1]);
            }
            return $number;
        } else {
            return "";
        }
    }

Usage Example

 public function testExtractPossibleNumber()
 {
     // Removes preceding funky punctuation and letters but leaves the rest untouched.
     $this->assertEquals("0800-345-600", PhoneNumberUtil::extractPossibleNumber("Tel:0800-345-600"));
     $this->assertEquals("0800 FOR PIZZA", PhoneNumberUtil::extractPossibleNumber("Tel:0800 FOR PIZZA"));
     // Should not remove plus sign
     $this->assertEquals("+800-345-600", PhoneNumberUtil::extractPossibleNumber("Tel:+800-345-600"));
     // Should recognise wide digits as possible start values.
     $this->assertEquals(pack("H*", 'efbc90') . pack("H*", 'efbc92') . pack("H*", 'efbc93'), PhoneNumberUtil::extractPossibleNumber(pack("H*", 'efbc90') . pack("H*", 'efbc92') . pack("H*", 'efbc93')));
     // Dashes are not possible start values and should be removed.
     $this->assertEquals(pack("H*", 'efbc91') . pack("H*", 'efbc92') . pack("H*", 'efbc93'), PhoneNumberUtil::extractPossibleNumber("Num-" . pack("H*", 'efbc91') . pack("H*", 'efbc92') . pack("H*", 'efbc93')));
     // If not possible number present, return empty string.
     $this->assertEquals("", PhoneNumberUtil::extractPossibleNumber("Num-...."));
     // Leading brackets are stripped - these are not used when parsing.
     $this->assertEquals("650) 253-0000", PhoneNumberUtil::extractPossibleNumber("(650) 253-0000"));
     // Trailing non-alpha-numeric characters should be removed.
     $this->assertEquals("650) 253-0000", PhoneNumberUtil::extractPossibleNumber("(650) 253-0000..- .."));
     $this->assertEquals("650) 253-0000", PhoneNumberUtil::extractPossibleNumber("(650) 253-0000."));
     // This case has a trailing RTL char.
     $this->assertEquals("650) 253-0000", PhoneNumberUtil::extractPossibleNumber("(650) 253-0000" . pack("H*", 'e2808f')));
 }
PhoneNumberUtil