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