Created
January 31, 2012 15:42
-
-
Save TMcManus/1711143 to your computer and use it in GitHub Desktop.
Number Formatting Functions
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
<?php | |
/** | |
* This function will take a number in a variety of formats and reformat | |
* the number to be in E.164 format. Currently this is really, really ugly, | |
* but it works. substr() based function might also work. Currently US only. | |
*/ | |
function normalizeNumber($number) { | |
$number = trim($number); | |
$number = preg_replace("/[^A-Za-z0-9]/", '', $number); | |
$number = preg_replace("/\\d{10}$/u", ",+1$0", $number); | |
$number = explode(',', $number); | |
return $number[1]; | |
} | |
/** | |
* This function will take an E.164 formatted number and put spaces | |
* in between each digit so that Twilio's <Say> reads it like a human would. | |
*/ | |
function spaceNumber($number) { | |
$number = trim($number); | |
$number = preg_replace("/[^A-Za-z0-9]/", '', $number); | |
$number = str_split($number); | |
$number = implode(' ', $number); | |
return $number; | |
} |
Trenton, I've made some stylist changes here: https://gist.github.com/1713519
Notice the format of the comments and the removal of the ending ?> tag. We remove the ending tag so we don't accidentally get whitespace into our code.
Thank you for the stylistic corrections! Now as soon as I can figure out how to merge branches...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For better or worse, here's what we use in OpenVBX: https://github.com/twilio/OpenVBX/blob/master/OpenVBX/helpers/format_helper.php#L24-70