Formatting Phone Numbers in PHP

Formatting Phone Numbers in PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Formatting Phone Numbers in PHP

Converting raw international phone numbers into a consistent, display-friendly format is a common requirement when building communication applications. The specific challenge of transforming an input like +11234567890 into 123-456-7890 for database storage or comparison involves more than just applying a simple regex replacement; it requires understanding the E.164 standard and the nuances of how different regions format their local digits.

The input string +11234567890 represents a US number in E.164 notation, where the leading + indicates an international dialing prefix, followed by the country code (1 for North America) and the subscriber number. However, your requirement to output 123-456-7890 suggests you need to strip the leading country code and apply a specific US local format. This creates an immediate data integrity risk: if your database expects numbers in E.164 format for global compatibility but you are storing localized versions, lookups across different regions will fail.

If you must adhere to this specific formatting logic due to legacy code constraints, the most straightforward approach in vanilla PHP is to use preg_replace. The goal is to remove the leading + and the country code, then insert hyphens at fixed positions assuming a 7-digit local number. Here is a basic implementation:

function formatPhoneNumber($input) {
    // Remove the leading '+' and any non-digits
    $clean = preg_replace('/[^0-9]/', '', $input);
    
    // Ensure we are working with the expected length for this specific region
    if (strlen($clean) == 10) {
        return substr($clean, -7) . '-' . substr($clean, -4, 3);
    }
    
    return $clean;
}

$formatted = formatPhoneNumber('+11234567890'); 
// Output: 123-456-7890

This function assumes the input is strictly a US number. If your SMS app handles international traffic, stripping the country code blindly introduces ambiguity. A number like +44 7911 123456 (UK) would become 791-112 if applied to this logic, which renders it useless for any subsequent lookup.

The real solution often lies in using a dedicated library rather than writing custom regex patterns. The telnet or libphonenumber libraries provide robust parsing and formatting capabilities that adhere to the global E.164 standard. While these require Composer installation and add a dependency, they prevent the "it works on my machine" issue caused by region-specific formatting assumptions.

composer require robthree/telnet
// Or alternatively for full international support:
// composer require eka/libphonenumber-for-php

Using a library like robthree/telnet allows you to normalize the input to E.164 first, then format it specifically for display if needed. However, since your constraint is to store the localized format in MySQL without altering existing code, you face a trade-off between data normalization and application flexibility.

Storing phone numbers as strings with embedded formatting characters (hyphens, spaces) can complicate database queries. Indexes on such columns become less effective for range queries or simple equality checks if the data isn't normalized. A better architectural pattern is to store the number in E.164 format (e.g., +11234567890) within the database and only apply the transformation 123-456-7890 at the application layer when displaying data to the user or sending SMS messages. This decouples your storage logic from your presentation logic, making future migrations significantly easier.

If you are using Laravel, this abstraction is built-in. You can utilize the Str::of() facade combined with helper packages to handle these transformations cleanly without polluting your core models. If your project is standalone PHP, consider creating a dedicated service class for phone handling to keep your logic isolated from business rules.

Ultimately, if you must store the formatted string, ensure that every piece of code reading that database column accounts for the specific XXX-XXX-XXXX pattern. Use strict validation on the application side before inserting data to prevent malformed entries from slipping into the database.

Our network