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
<?php function format_number($number_data) { //make sure the number_data is actually a number_data if(is_numeric($number_data)){ //if number_data doesn't start with a 0 or a 4 add a 0 to the start. if($number_data[0] != 0 && $number_data[0] != 4){ $number_data = "0".$number_data; } //if number_data starts with a 0 replace with 4 if($number_data[0] == 0){ $number_data[0] = str_replace("0","4",$number_data[0]); $number_data = "4".$number_data; } //remove any spaces in the number_data $number_data = str_replace(" ","",$number_data); //return the number_data return $number_data; //number_data is not a number_data } else { //return nothing return false; } } ?>
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.