I do appreciate the feedback, and admittedly the function indentation could use a little clean up, however as laserlight and leatherback's last two posts along with the aforementioned sticky itself illustrate -- in this context it is yet largely a matter of preference. I also found a related article titled Proper Linux Kernel Coding Style which is more in line with laserlight's style example of my code suggesting that...
if (x is true) {
we do y
}
with the exception being functions where in we do something more like leatherback's example...
function($phone)
{
body of function
}
At any rate, I took another swing at it before reading the last two posts and this was my best shot (with explanitory comments)
<?php
$number ="(800).606-HOME ext-1234";
$numberx ="(800).606-HOME";
function format_phone($phone)
{
$match=array(" ","-",".","(",")");//common phone delimiters (),-, 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
?>
Which I would like to think I could submit to the code critique forum without undue embarrassment?