Skip to content

Instantly share code, notes, and snippets.

@TMcManus
Created January 31, 2012 15:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TMcManus/1711143 to your computer and use it in GitHub Desktop.
Save TMcManus/1711143 to your computer and use it in GitHub Desktop.
Number Formatting Functions
<?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;
}
@Gipetto
Copy link

Gipetto commented Jan 31, 2012

@kyleconroy
Copy link

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.

@TMcManus
Copy link
Author

TMcManus commented Feb 1, 2012

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