php
53 lines · 8 steps
Normalizing phone numbers to E.164 in PHP
A static helper cleans arbitrary phone input, applies a country prefix, and validates it against the E.164 format.
Explained by
highlit
1<?php
2
3namespace App\Support;
4
5use InvalidArgumentException;
6
7final class PhoneNumber
8{
9 private const COUNTRY_PREFIXES = [
10 'US' => '1',
11 'GB' => '44',
12 'FR' => '33',
13 'DE' => '49',
14 'IN' => '91',
15 ];
16
17 public static function toE164(string $input, string $defaultRegion = 'US'): string
18 {
19 $trimmed = trim($input);
20 $hasPlus = str_starts_with($trimmed, '+');
21 $digits = preg_replace('/\D+/', '', $trimmed);
22
23 if ($digits === '' || strlen($digits) < 8) {
24 throw new InvalidArgumentException("Phone number '{$input}' has too few digits.");
25 }
26
27 if (!$hasPlus) {
28 $digits = self::applyDefaultRegion($digits, $defaultRegion);
29 }
30
31 if (strlen($digits) > 15) {
32 throw new InvalidArgumentException("Phone number '{$input}' exceeds the E.164 maximum length.");
33 }
34
35 return '+' . $digits;
36 }
37
38 private static function applyDefaultRegion(string $digits, string $region): string
39 {
40 $prefix = self::COUNTRY_PREFIXES[strtoupper($region)]
41 ?? throw new InvalidArgumentException("Unsupported default region '{$region}'.");
42
43 if ($region === 'US' && strlen($digits) === 11 && str_starts_with($digits, '1')) {
44 return $digits;
45 }
46
47 if (str_starts_with($digits, $prefix) && strlen($digits) > 10) {
48 return $digits;
49 }
50
51 return $prefix . ltrim($digits, '0');
52 }
53}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Strip formatting to raw digits first, then reason about the number as a canonical string.
- 2Length bounds and a known-prefix lookup catch most malformed input before it propagates.
- 3Handling the leading-plus case separately lets you infer a region only when the caller didn't specify one.
Related explainers
php
<?php class NameParser {
Parsing a full name into components in PHP
string-parsing
arrays
normalization
Intermediate
8 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
php
<?php namespace App\Services;
Building a cached daily leaderboard in Laravel
caching
aggregation
eager-loading
Intermediate
9 steps
php
<?php final class RotatingFileLogger {
How a rotating file logger works in PHP
logging
file-rotation
io
Intermediate
9 steps
go
package middleware import ( "net/http"
Per-plan export limits in Gin middleware
middleware
rate-limiting
authorization
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/normalizing-phone-numbers-to-e-164-in-php-explained-php-47cd/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.