Menu Close

Change Phone Numbers Format In PHP

Codeamend

Here, we will see how to change phone number format using PHP. Check the below code to change phone number format in php preg_replace.

Example:

<?php
    function phone_number_format($number){
        $number = preg_replace("/[^\d]/","",$number);
        $length_of_number = strlen($number);
        if($length_of_number == 10) {
            echo $number = "Mobile Number : " . preg_replace("/^1?(\d{3})(\d{3})(\d{4})$/", "$1-$2-$3", $number);
        }else{
            echo "Invalid Mobile Number";
        }
    }
    phone_number_format(9876543210);
?> 

Output:

Mobile Number : 987-654-3210 
Posted in PHP

You can also read...