- preg_match: documentation ( source)
- str_replace: documentation ( source)
- in_array: documentation ( source)
<?php
function ReformatPhoneNumber($n){
if(!preg_match('/\\d+\\-\\d+\\s\\d+/',$n))throw new Exception('Invalid phone number');
$n=str_replace(array('-',' '), '', $n);
if(!in_array(strlen($n), array(7,12)))throw new Exception('Invalid phone number');
return $n;
}
echo ReformatPhoneNumber('-2352235 32665');