PHP

Normalize Phone Number to E.164

admin by @admin ADMIN
29m ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
A minimal E.164 normalizer for US/CA numbers — strips formatting, prepends "+1" if missing, and validates the digit count. For full international support, reach for giggsey/libphonenumber-for-php.
PHP
Raw
<?php
function normalizeUSPhone(string $input): ?string {
    $digits = preg_replace('/\D+/', '', $input);
    if (strlen($digits) === 10) {
        return '+1' . $digits;
    }
    if (strlen($digits) === 11 && $digits[0] === '1') {
        return '+' . $digits;
    }
    return null;     // not a recognizable US/CA number
}

echo normalizeUSPhone('(512) 555-0199');    // +15125550199
echo normalizeUSPhone('1-512-555-0199');    // +15125550199
echo normalizeUSPhone('+15125550199');      // +15125550199
var_dump(normalizeUSPhone('12345'));        // null
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.