The PhoneNumberMatcher class finds and extracts telephone numbers from unstructured text. It implements the Iterator interface, allowing you to iterate over all phone numbers found in the text.
Vanity numbers (phone numbers using alphabetic digits such as ‘1-800-SIX-FLAGS’) are not currently supported.
Instances are created using PhoneNumberUtil::findNumbers():
$phoneNumberUtil = \libphonenumber\PhoneNumberUtil::getInstance();$text = "Hi, can you ring me at 1430 on 0117 496 0123. Thanks!";$phoneNumberMatcher = $phoneNumberUtil->findNumbers($text, 'GB');
The default country/region code to assume for phone numbers not written in international format. Use null or "ZZ" to only match numbers with a leading plus sign.
use libphonenumber\Leniency;$matcher = $phoneUtil->findNumbers($text, 'US', Leniency::POSSIBLE());
The most permissive level. Matches any sequence of digits that could possibly be a phone number, with minimal validation. This may produce false positives.Use when: You want maximum recall and are willing to manually filter results.
Matches phone numbers that pass validation checks. This is the default and recommended level for most use cases.Use when: You want a good balance between precision and recall.
Matches valid phone numbers where the grouping of digits matches a known formatting pattern for the region.Use when: You want to avoid matching numbers that are formatted incorrectly for the region.
The strictest level. Matches only phone numbers where the digit grouping exactly matches a standard formatting pattern, including all separators.Use when: You need the highest precision and only want perfectly formatted numbers.
use libphonenumber\Leniency;// Find any possible phone numbers$matcher = $phoneUtil->findNumbers( "Numbers: 555-1234, 123-4567", 'US', Leniency::POSSIBLE());// May find: 555-1234, 123-4567 (even if invalid)