Format Phone Number using PHP

Today, We want to share with you php phone number format.In this post we will show you php international phone number validation, hear for PHP Mobile number formatting we will give you demo and example for implement.In this post, we will learn about How To Allow Only 10 Digit Mobile Number Validation Using Javascript With Jquery with an example.

php format phone numbers

Below is a tiny and simple PHP function that will take any 10 digit mobile number and convert it into the following moblie or phone format: 982-062-41**.

function phone_number_format($mobile_number) {
  //here verify and simple Allow only number Digits, remove all any types of other characters.
  $mobile_number = preg_replace("/[^\d]/","",$mobile_number);
 
  // get mobile_number length.
  $length = strlen($mobile_number);
 
 // if mobile_number = 10
 if($length == 10) {
  $mobile_number = preg_replace("/^1?(\d{3})(\d{3})(\d{4})$/", "$1-$2-$3", $mobile_number);
 }
  
  return $mobile_number;

}

I hope you get an idea about php validate phone number.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment