Skip to content
Related Articles
Open in App
Not now

Related Articles

How to format Phone Numbers in PHP ?

Improve Article
Save Article
  • Last Updated : 25 May, 2021
Improve Article
Save Article

In this article, we will learn how to format phone numbers using PHP. When we need to store a phone number then we store the formatting phone number. Using PHP, we can easily format phone numbers.

Approach: In this article, we will format the phone number using the preg_match() method. We can use this method to format phone numbers. This function has special characteristics of finding specified patterns from string. 

PHP code: The following is the complete code to format phone numbers using preg_match().


// Create a formatting function
function formatting($phone){
    // Pass phone number in preg_match function
    $phone, $value)) {
        // Store value in format variable
        $format = $value[1] . '-'
            $value[2] . '-' . $value[3];
    else {
        // If given number is invalid
        echo "Invalid phone number <br>";
    // Print the given format
    echo("$format" . "<br>");
// Call the function


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!