Skip to content

Instantly share code, notes, and snippets.

@ManUtopiK
Last active August 29, 2015 14:13
Show Gist options
  • Save ManUtopiK/51928af258b06e1b3290 to your computer and use it in GitHub Desktop.
Save ManUtopiK/51928af258b06e1b3290 to your computer and use it in GitHub Desktop.
Format french phone number
/**
* Format french phone number
* @param [string] $tel Phone number to format
* @return string Conventional human readable phone number
*/
function FormatTel($tel) {
$t = ereg_replace("[^0-9]", "", $tel);
if (strlen($t) === 10) {
return substr(chunk_split($t, 2, " "), 0, -1);
} else if (preg_match('/^\(?\+?33/', $tel)) {
if (strlen($t) === 11) return '+33 ' . $t[2] . ' ' . substr(chunk_split(substr($t, 3), 2, " "), 0, -1);
}
return $tel;
}
echo FormatTel("+33 1 23 45 67 89"); //retourne 01 23 45 67 89
echo '<br>';
echo FormatTel("+33 1.23-45 67 89"); //retourne 01 23 45 67 89
echo '<br>';
echo FormatTel("(+33) 123456789"); //retourne 01 23 45 67 89
echo '<br>';
echo FormatTel("01.23.45.67.89"); //retourne 01 23 45 67 89
echo '<br>';
echo FormatTel("01-23-45-67-89"); //retourne 01 23 45 67 89
echo '<br>';
echo FormatTel("33 12345.6890"); //retourne 33 12345.6890
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment