The PhoneNumberMatcher class finds and extracts phone numbers from text strings. This is useful for processing user-generated content, parsing documents, or extracting contact information.
use libphonenumber\PhoneNumberUtil;$phoneUtil = PhoneNumberUtil::getInstance();$text = "Hi, can you ring me at 1430 on 0117 496 0123. Thanks!";// Find all numbers in the text for GB region$matcher = $phoneUtil->findNumbers($text, 'GB');foreach ($matcher as $phoneNumberMatch) { $number = $phoneNumberMatch->number(); // Display formatted number echo $phoneUtil->format( $number, \libphonenumber\PhoneNumberFormat::INTERNATIONAL ); // Output: +44 117 496 0123 // Get the raw text that was matched echo $phoneNumberMatch->rawString(); // Output: 0117 496 0123 // Get the position in the text echo "Found at position: " . $phoneNumberMatch->start(); // Output: Found at position: 30}
The Leniency parameter controls how strict the matching should be:
POSSIBLE
VALID (Default)
STRICT_GROUPING
EXACT_GROUPING
Most permissive - Matches anything that could possibly be a phone number:
use libphonenumber\Leniency\Possible;$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();$text = "Call 1234567890 or 123-456-7890";$matcher = $phoneUtil->findNumbers( $text, 'US', Possible::getInstance(), PHP_INT_MAX);// Will match both numbers even without proper formattingforeach ($matcher as $match) { echo $match->rawString() . "\n";}
Use when you want to catch all potential phone numbers, even if they might be false positives.
Recommended - Matches only valid phone numbers:
use libphonenumber\Leniency\Valid;$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();$text = "Call +1 (650) 253-0000 for support";$matcher = $phoneUtil->findNumbers( $text, 'US', Valid::getInstance(), PHP_INT_MAX);// Matches only if the number is validforeach ($matcher as $match) { echo $match->rawString(); // +1 (650) 253-0000}
This is the default and works well for most use cases.
Stricter - Validates digit grouping:
use libphonenumber\Leniency\StrictGrouping;$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();$text = "Valid: (650) 253-0000, Invalid: (65) 025-30000";$matcher = $phoneUtil->findNumbers( $text, 'US', StrictGrouping::getInstance(), PHP_INT_MAX);// Only matches numbers with proper grouping// Won't match the second number
Strictest - Requires exact formatting:
use libphonenumber\Leniency\ExactGrouping;$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();$text = "Call (650) 253-0000 exactly as formatted";$matcher = $phoneUtil->findNumbers( $text, 'US', ExactGrouping::getInstance(), PHP_INT_MAX);// Most strict matching - perfect formatting required
May miss valid numbers if they’re not formatted exactly right.
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();// Find at most 5 numbers$matcher = $phoneUtil->findNumbers( $text, 'US', \libphonenumber\Leniency\Valid::getInstance(), 5 // maxTries);foreach ($matcher as $match) { // Process up to 5 matches}
Set maxTries to prevent excessive processing on very long documents.
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();// Could be a phone number or a time$text = "Meet at 1430 or call 0117 496 0123";$matcher = $phoneUtil->findNumbers($text, 'GB');foreach ($matcher as $match) { $raw = $match->rawString(); // "1430" might be matched as a phone number // Validate the match $number = $match->number(); if ($phoneUtil->isValidNumber($number)) { echo "Valid: $raw\n"; } else { echo "Possibly not a phone number: $raw\n"; }}
Always validate matched numbers, especially with more lenient matching modes.