Skip to content

Instantly share code, notes, and snippets.

@kyleconroy
Forked from TMcManus/gist:1711143
Created January 31, 2012 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyleconroy/1713519 to your computer and use it in GitHub Desktop.
Save kyleconroy/1713519 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment