Join WhatsApp ChannelJoin Now

Phone Numbers Format Change In PHP

This post was last updated on December 9th, 2023 at 06:57 am

Hi Dev,

Today, i we will show you phone numbers format change in PHP. This article will give you simple example of phone numbers format change in PHP. you will phone numbers format change in PHP. In this article, we will implement a phone numbers format change in PHP.

So let’s follow few step to create example of phone numbers format change in PHP.

Example 1

<?php

    function phone_number_format($number){

        // Allow only Digits, remove all other characters.
        $number = preg_replace("/[^\d]/","",$number);
     
        // get number length.
        $length_of_number = strlen($number);
     
        // if number = 10
        if($length_of_number == 10) {
            echo $number = "Mobile Number is : " . preg_replace("/^1?(\d{3})(\d{3})(\d{4})$/", "$1-$2-$3", $number);
        }else{
            echo "Invalid Number";
        }

    }

    phone_number_format(9423908056);

?>

Output:

Mobile Number is : 942-390-8056

Example 2

<?php 

    $data = '+11234567890';

    if(preg_match( '/^\+\d(\d{3})(\d{3})(\d{4})$/', $data,  $matches ) )
    {
        $result = $matches[1] . '-' .$matches[2] . '-' . $matches[3];
        echo "Mobile Number is : ".$result;

    }else{
        echo "Invalid Number";
    }

?>

Output:

Mobile Number is : 123-456-7890

I hope it will assist you…

Recommended Posts