How to Format Phone Numbers in PHP
-
Use the
preg_match()
Function to Format Phone Numbers in PHP -
Use the
sprintf()
Function to Format Phone Numbers in PHP
In this article, we will talk about different methods to format phone numbers using PHP. We will format the phone number from +13335092344
to 333-509-2344
.
Use the preg_match()
Function to Format Phone Numbers in PHP
We can use the built-in function preg_match()
to format phone numbers. This function searches a specified pattern from a string. The correct syntax to use this function is as follows.
preg_match($pattern, $inputString, $matches, $flag, $offset);
The built-in function preg_match()
has five parameters. The details of its parameters are as follows
Parameters | Description | |
---|---|---|
$pattern |
mandatory | It is the pattern that we want to check in the given string. |
$inputString |
mandatory | It is the string for which we want to search the given pattern. |
$matches |
optional | If this parameter is given, then the function stores the result of the matching process in it. |
$flags |
optional | This parameter has two options: PREG_OFFSET_CAPTURE and PREG_UNMATCHED_AS_NULL . You can read its description here. |
$offset |
optional | It tells the function of where to start the matching process. Usually, the search starts from the beginning. |
This function returns a boolean variable. It returns true if the given pattern exists. After extracting the numbers, we will print them in the required format.
The program below shows the way by which we can use PHP preg_match()
function to format phone numbers.
<?php
$number = '+12333509234';
echo("The original number is $number.\n");
if( preg_match( '/^\+\d(\d{3})(\d{3})(\d{4})$/', $number, $matches ) )
{
$result = $matches[1] . '-' .$matches[2] . '-' . $matches[3];
}
echo("The formatted number is $result.");
?>
We have used /^\+\d(\d{3})(\d{3})(\d{4})$/
pattern to extract numbers from the string. The explanation of this pattern is as follows.
^
is the symbol to match the start of the string.\+
matches the+
in the string.+
is a special character in PHP to match one or more times of the preceding pattern. Therefore, we need to add\
before+
to escape this special character.\d
is used to match the single digit number from 0-9.\d{3}
means that it will match three consecutive numbers.
Output:
The original number is +12333509234.
The formatted number is 233-350-9234.
Use the sprintf()
Function to Format Phone Numbers in PHP
We can also use the sprintf()
function to format phone numbers in PHP. This function gives several formatting patterns to format strings. We will extract the numbers using substr()
function in several strings. After that, we will combine these strings in the required format using the sprintf()
function. The correct syntax to use this function is as follows.
sprintf($formatString, $string1, $string2, ..., $stringN)
The sprintf()
function accepts N+1 parameters. The detail of its parameters is as follows
Parameters | Description | |
---|---|---|
$formatString |
mandatory | It is the format that will be applied to the given string or strings. |
$string1 , $string2 , $stringN |
mandatory | It is the string that we want to format. At least one string is mandatory. |
The function returns the formatted string. We will use the format %s-%s-%s
to combine the numeric strings. The program that combines two strings is as follows.
<?php
$number = '+12333509234';
echo("The original number is $number.\n");
$result = sprintf("%s-%s-%s",
substr($number, 2, 3),
substr($number, 5, 3),
substr($number, 8));
echo("The formatted number is $result.");
?>
Output:
The original number is +12333509234.
The formatted number is 233-350-9234.