note 110082 added to function.substr
| From: | odc311atgmaildotcom | Date: | Mon, 17 Sep 2012 18:22:49 +0000 |
| Subject: | note 110082 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190849@lists.php.net to get a copy of this message | ||
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;
}
?>
----
Server IP: 192.41.42.26
Probable Submitter: 199.15.131.226
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- https://master.php.net/note/edit/110082
Del: integrated -- https://master.php.net/note/delete/110082/integrated
Del: useless -- https://master.php.net/note/delete/110082/useless
Del: bad code -- https://master.php.net/note/delete/110082/bad+code
Del: spam -- https://master.php.net/note/delete/110082/spam
Del: non-english -- https://master.php.net/note/delete/110082/non-english
Del: in docs -- https://master.php.net/note/delete/110082/in+docs
Del: other reasons-- https://master.php.net/note/delete/110082
Reject -- https://master.php.net/note/reject/110082
Search -- https://master.php.net/manage/user-notes.php