How to Format Phone Numbers in PHP?

Formatting phone numbers in PHP is a common task, especially when working on web applications that display contact information.

Properly formatted phone numbers are not only more readable but also ensure a consistent and professional appearance.

In this article, we'll explore how to format phone numbers in PHP using various techniques.

Method 1: Using Regular Expressions

One way to format phone numbers is to use regular expressions to match and reformat the numbers.

Regular expressions allow you to define patterns that match specific formats within strings.

Here's an example of how to format a phone number with a regular expression:

function formatPhoneNumber($phoneNumber) {
    // Remove all non-digit characters
    $phoneNumber = preg_replace('/\D/', '', $phoneNumber);

    // Add the desired formatting
    if (strlen($phoneNumber) == 10) {
        return preg_replace('/(\d{3})(\d{3})(\d{4})/', '($1) $2-$3', $phoneNumber);
    } elseif (strlen($phoneNumber) == 11) {
        return preg_replace('/(\d)(\d{3})(\d{3})(\d{4})/', '$1 ($2) $3-$4', $phoneNumber);
    } else {
        return $phoneNumber; // Cannot format, return as is
    }
}

$phoneNumber = "1234567890";
$formattedNumber = formatPhoneNumber($phoneNumber);
echo "Formatted Phone Number: " . $formattedNumber;

Output:

Formatted Phone Number: (123) 456-7890

In this example, the formatPhoneNumber() function uses regular expressions to remove non-digit characters and format the phone number as "(123) 456-7890." It can handle both 10-digit and 11-digit numbers.


Method 2: Using substr() and String Concatenation

Another approach to format phone numbers is by using string manipulation functions like substr() and concatenation.

Here's an example of how to format a phone number using substr():

function formatPhoneNumber($phoneNumber) {
    $phoneNumber = preg_replace('/\D/', '', $phoneNumber);

    if (strlen($phoneNumber) == 10) {
        return "(" . substr($phoneNumber, 0, 3) . ") " . substr($phoneNumber, 3, 3) . "-" . substr($phoneNumber, 6, 4);
    } elseif (strlen($phoneNumber) == 11) {
        return $phoneNumber[0] . " (" . substr($phoneNumber, 1, 3) . ") " . substr($phoneNumber, 4, 3) . "-" . substr($phoneNumber, 7, 4);
    } else {
        return $phoneNumber; // Cannot format, return as is
    }
}

$phoneNumber = "123-456-7890";
$formattedNumber = formatPhoneNumber($phoneNumber);
echo "Formatted Phone Number: " . $formattedNumber;

Output:

Formatted Phone Number: (123) 456-7890

In this example, the formatPhoneNumber() function uses substr() to extract and concatenate the desired parts of the phone number to create the formatting.


Method 3: Using number_format()

You can also use the number_format() function to format phone numbers. While this function is primarily designed for formatting numbers, it can be adapted for phone numbers.

Here's an example of how to format a phone number using number_format():

function formatPhoneNumber($phoneNumber) {
    $phoneNumber = preg_replace('/\D/', '', $phoneNumber);

    if (strlen($phoneNumber) == 10) {
        $formattedNumber = number_format($phoneNumber, 0, '', ''); // Format as plain number
        return "(".substr($formattedNumber, 0, 3).") ".substr($formattedNumber, 3, 3)."-".substr($formattedNumber, 6, 4);
    } elseif (strlen($phoneNumber) == 11) {
        $formattedNumber = number_format($phoneNumber, 0, '', ''); // Format as plain number
        return $formattedNumber[0]." (".substr($formattedNumber, 1, 3).") ".substr($formattedNumber, 4, 3)."-".substr($formattedNumber, 7, 4);
    } else {
        return $phoneNumber; // Cannot format, return as is
    }
}

$phoneNumber = "1-234-567-8901";
$formattedNumber = formatPhoneNumber($phoneNumber);
echo "Formatted Phone Number: " . $formattedNumber;

Output:

Formatted Phone Number: (234) 567-8901

In this example, the formatPhoneNumber() function first removes non-digit characters, then uses number_format() to format the number, and finally applies the desired formatting.


Conclusion:

Formatting phone numbers in PHP is essential for maintaining a consistent and professional appearance in your applications. The choice of method for formatting depends on your specific needs and preferences.

Regular expressions provide powerful pattern matching capabilities, while string manipulation functions and number_format() offer alternative approaches to formatting phone numbers.