- preg_match: documentation ( source)
- str_replace: documentation ( source)
<?php
function ReformatPhoneNumber($number)
{
$isValid = preg_match('/^(\d[ |-]*){6,11}\d$/i', $number, $result);
if(!$isValid) {
throw new Exception("Invalid phone number");
}
return str_replace(array(' ' , '-'), array('', ''), $number);
}