Posted inTechnology / php

php format phone number with dashes

Today, We want to share with you php format phone numbers.In this post we will show you php format phone number with dashes, hear for Convert mobile number to international format dynamically with php we will give you demo and example for implement.In this post, we will learn about php phone number format with an example.

Formatting Phone Numbers in PHP

here i will to learn format international (10+ digit), old school (7 digit), non-international (10 digit)

$phone_number = '+10987654321';

if(  preg_match( '/^\+\d(\d{3})(\d{3})(\d{4})$/', $phone_number,  $matches ) )
{
    $output = $matches[1] . '-' .$matches[2] . '-' . $matches[3];
    return $output;
}

Format a Telephone Number – PHP

function formatPhoneNumber($number_data) {
    $number_data = preg_replace('/[^0-9]/','',$number_data);

//format international (10+ digit),
    if(strlen($number_data) > 10) {
        $countryCode = substr($number_data, 0, strlen($number_data)-10);
        $areaCode = substr($number_data, -10, 3);
        $check_three_number = substr($number_data, -7, 3);
        $check_four_number = substr($number_data, -4, 4);

        $number_data = '+'.$countryCode.' ('.$areaCode.') '.$check_three_number.'-'.$check_four_number;
    }
    else if(strlen($number_data) == 10) { //non-international (10 digit) 
        $areaCode = substr($number_data, 0, 3);
        $check_three_number = substr($number_data, 3, 3);
        $check_four_number = substr($number_data, 6, 4);

        $number_data = '('.$areaCode.') '.$check_three_number.'-'.$check_four_number;
    }
    else if(strlen($number_data) == 7) { //old school (7 digit)
        $check_three_number = substr($number_data, 0, 3);
        $check_four_number = substr($number_data, 3, 4);

        $number_data = $check_three_number.'-'.$check_four_number;
    }

    return $number_data;
}

Example


I hope you get an idea about php phone number format.
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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype