0

I'm trying to format a 10 digit phone number 1234567890 so that it would look like 123-456-7890.

Note: my form simply takes a input type text and inserted that to my database as the 10 digit above. But now if I want to echo this out, a correct format is what I need to show. The "design" of the form is not so much what I need help on but rather the "formatting" of the data. If the data was inserted as a non formatted 10 digit phone number without slashes or dashes in the the beginning of data input, or the way it was inserted into the database.

In other words, how would I format the unformatted or raw data in PHP so that it is echoed in certain format.

Nico
  • 12,493
  • 5
  • 42
  • 62
mythoslife
  • 203
  • 1
  • 6
  • 20

3 Answers3

2

You can get it done for any format of US phone numbers by using this -

$phone = "1234567890";
$numbers_only = preg_replace("/[^\d]/", "", $phone);
echo preg_replace("/^1?(\d{3})(\d{3})(\d{4})$/", "$1-$2-$3", $numbers_only);

Output 123-456-7890

Sujit Agarwal
  • 12,348
  • 11
  • 48
  • 79
  • this works too. $number = $data->phone; then, $phone = "$number[0]$number[1]$number[2]-$number[3]$number[4]$number[5]-$number[6]$number[7]$number[8]$number[9]"; – mythoslife Jan 09 '14 at 05:27
  • yeah, it will work, but just suppose the user entered the number with some spaces or with country code. Just in case of any kind of format provided by the user, this code should work. – Sujit Agarwal Jan 09 '14 at 05:29
  • true I supposed I should have formatted the data input in the beginning as well. I sometimes get confused about PHP there's so many ways to do one thing. And there's so many best practices. – mythoslife Jan 09 '14 at 05:34
  • It's absolutely normal to be confused in beginning, but once you go on the proper path, you'll feel awesome... :) If this solution worked for you, dont forget to mark it as correct for other readers' reference :) – Sujit Agarwal Jan 09 '14 at 05:38
1

I have made simple example for this problem using string functions split,implode etc. If you are using String for storing 10 digit number this will work for that ..

<?php
   $number ="1234567890";
   $arr=array();
   $arr=str_split($number,3);
   $arr[2]=$arr[2].$arr[3];
   unset($arr[3]);
   $phone_no = implode("-", $arr);
   print_r($phone_no);
?>

Output:

123-456-7890
Straw Hat
  • 902
  • 14
  • 38
  • @D_Valibhar I did it so that each characters is echoed as index. $phone = "$number[0]$number[1]$number[2]-$number[3]$number[4]$number[5]-$number[6]$number[7]$number[8]$number[9]"; – mythoslife Jan 09 '14 at 05:37
  • @mythoslife if you take a String simply and use above code it will work :D . Still happy that you found some solution by your own. Great – Straw Hat Jan 09 '14 at 05:42
0

DEMO : https://eval.in/87514

Try This:

$replace = array('-',' '); //set your unexpected character here it will be replaced
$str = '1 234567890';
$str = str_replace($replace,'',$str);
$str = preg_replace("/[^\d]/", "", $str);//check now only have number in string
$p ="/(\d{3})(\d{3})(\d{4})/";
if (strlen($str) === 10){ //check exact length of your string
 echo $str= preg_replace($p,"$1-$2-$3",$str);//format your string 
}
else{
 echo "unmatched length!";
}
Awlad Liton
  • 9,366
  • 2
  • 27
  • 53