Hey All,
My first post for critique. Be gentle I'm fragile😉
Anyway, I've worked out the following few lines of code with the intent of normalizing the contact phone numbers in our db to better facilitate Skype calling (US, 10 Digit). I have to give credit (or blame) to the forum's own Weedpacket for what I think is the best bit...
$phone = strtr(strtolower($phone),
'abcdefghijklmnopqrstuvwxyz',
'22233344455566677778889999');
...for translating Mnemonic (spelled out ) phone numbers.
At that, the fourm's own laserlight suggested I post here for critique, so here goes...
<?php
$number ="(800).606-HOME ext-1234";
$numberx ="(800).606-HOME";
function format_phone($phone)
{
$match=array(" ","-",".","(",")");
//common phone delimeters (),-, and .
$numb = str_replace($match,$replace,$phone);
//strip non-alpha/numeric e.g "808606HOMEext1234"
$length =strlen($numb);
if($length >10){ // allow for extension i.e. ext-558 or x-1234 etc.
$numb=substr_replace($numb, ' ', 10, 0);
//now "808606HOME ext1234"
$ext = explode(" ", $numb);
$phone = $ext[0]; //e.g. 808606HOME
$extension = preg_replace('/\D/','',$ext[1]);//e.g."1234"
$extension= "x-".$extension; //e.g. "x-1234"
}
else{
$phone=$numb;//e.g $length !>10
}
$phone = strtr(strtolower($phone),
'abcdefghijklmnopqrstuvwxyz',
'22233344455566677778889999');
//e.g. "HOME" becomes "home" and is then translated to 4663
$phone=substr_replace($phone, '-', 3, 0);//e.g. $phone=800-6064663
$phone=substr_replace($phone, '-', 7, 0); e.g. $phone=800-606-4663
return $phone." ".$extension;
}
$phone_mobile = format_phone($number);
$phone_office = format_phone($numberx);
echo $phone_mobile;// returns 800-606-4663 x-1234
echo $phone_office;// returns 800-606-4663
?>