Last active
April 14, 2016 11:04
-
-
Save iamjoshellis/e6fb6b10174544f01d0a89e440e081e3 to your computer and use it in GitHub Desktop.
Tidy a phone number and make it into a formatted 'tel' link
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Formatted Phone Link | |
*/ | |
function format_phone_link($phone) { | |
$phone = preg_replace('/[^0-9]/', '', $phone); | |
$length = strlen($phone); | |
switch ($length) { | |
case 7: | |
$phone_formatted = preg_replace('/([0-9]{3})([0-9]{4})/', '$1 $2', $phone); | |
break; | |
case 10: | |
$phone_formatted = preg_replace('/([0-9]{3})([0-9]{3})([0-9]{4})/', '$1 $2 $3', $phone); | |
break; | |
case 11: | |
$phone_formatted = preg_replace('/([0-9]{4})([0-9]{3})([0-9]{4})/', '$1 $2 $3', $phone); | |
break; | |
} | |
return '<a href="tel:' . $phone .'">' . $phone_formatted . '</a>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment