note 110082 deleted from function.substr by aharvey
| From: | aharvey@php.net | Date: | Tue, 18 Sep 2012 02:01:17 +0000 |
| Subject: | note 110082 deleted from function.substr by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190852@lists.php.net to get a copy of this message | ||
Note Submitter: odc311 at gmail dot com
----
I was searching for a simple function to format phone numbers using a specified separator. I
couldn't find anything so I created this simple little gem. It can handle both US and
International phone numbers. Enjoy :)
<?php
function phoneNumber($n) {
// Set the digit separator
$sep = "-";
// determine the digit count for the number
$tl = strlen($n);
if($tl > 10) {
// get the country code
$country_code = substr($n,0,-10);
// Get the area code
$area_code = substr($n,($tl-10),-7);
// Get the prefix
$prefix = substr($n,($tl-7),3);
// Get the line number
$line = substr($n,-4);
// Assemble the number with the selected separator
$phone = "+" . $country_code . $sep . $area_code . $sep . $prefix . $sep . $line;
}
else {
// Get the area code
$area_code = substr($n,($tl-10),-7);
// Get the prefix
$prefix = substr($n,3,3);
// Get the line number
$line = substr($n,-4);
// Assemble the number with the selected separator
$phone = $area_code . $sep . $prefix . $sep . $line;
}
return $phone;
}
?>