- preg_match: documentation ( source)
- str_replace: documentation ( source)
<?php
function ReformatPhoneNumber($number) {
if(strlen($number) > 12 || strlen($number) < 7)
throw new Exception("Invalid phone number");
if(preg_match('/^\d+(\d-\d)?\d*(\d\s\d)?\d+$/', $number) === 1)
return str_replace(array('-', ' '), '', $number);
}
echo ReformatPhoneNumber('012345');