From 28f525380cc6c2a5aa99302c7845bca6ef9565bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Thu, 20 May 2021 14:47:49 +0200 Subject: [PATCH] Formatter: Add support for starting zero like 0123... --- src/PhoneNumberFormatter.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/PhoneNumberFormatter.php b/src/PhoneNumberFormatter.php index baeabe4..00e8f5a 100644 --- a/src/PhoneNumberFormatter.php +++ b/src/PhoneNumberFormatter.php @@ -15,6 +15,7 @@ final class PhoneNumberFormatter public static function fix(string $phone, int $region = 420): string { $phone = (string) preg_replace('/[^\d+]/', '', $phone); // remove spaces + $phone = (string) preg_replace('/^0([1-9])/', '$1', $phone); // remove starting zero if (preg_match('/^(?:0{2,}|\+)\s*([1-9]\d{2})\s*(.+)$/', $phone, $phoneRegionParser)) { $region = (int) ($phoneRegionParser[1] ?? throw new \LogicException('Invalid phone prefix.')); $phone = '+' . $region . ($phoneRegionParser[2] ?? '');