Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

what is wrong with this code?

function formatPhoneNumber($phoneNumber) {

$phoneNumber = preg_replace('/[^0-9]/','',$phoneNumber);

if(strlen($phoneNumber) > 10) {

$countryCode = substr($phoneNumber, 0, strlen($phoneNumber)-10);

$areaCode = substr($phoneNumber, -10, 3);

$nextThree = substr($phoneNumber, -7, 3);

$lastFour = substr($phoneNumber, -4, 4);

$phoneNumber = '+'.$countryCode.' ('.$areaCode.') '.$nextThree.'-'.$lastFour;

}

else if(strlen($phoneNumber) == 10) {

$areaCode = substr($phoneNumber, 0, 3);

$nextThree = substr($phoneNumber, 3, 3);

$lastFour = substr($phoneNumber, 6, 4);

$phoneNumber = '('.$areaCode.') '.$nextThree.'-'.$lastFour;

}

else if(strlen($phoneNumber) == 7) {

$nextThree = substr($phoneNumber, 0, 3);

$lastFour = substr($phoneNumber, 3, 4);

$phoneNumber = $nextThree.'-'.$lastFour;

}

return $phoneNumber;

}

For some reason I get the number back as it was ie.12345467890 instead of (123)456-7890

Any help is appreciated

Mr A

1 Answer

Relevance
  • 7 years ago
    Favorite Answer

    If you're using vba, set the return value to the function name

    formatPhoneNumber=$phonenumber;

    also, use a different variable for setting the value, $phonenumber as this leads to confusion

Still have questions? Get your answers by asking now.