- preg_match: documentation ( source)
- str_replace: documentation ( source)
<?php
function ReformatPhoneNumber($number)
{
if (preg_match('/^(\d[ -]?){7,12}$/', $number))
return str_replace(array(' ','-'), '', $number);
throw new Exception('Invalid phone number');
}
echo ReformatPhoneNumber('012-345 67');