Here's a PHP function that will properly format a phone number, so that the first space is after the area code and the rest is split evenly into two parts. The code here is specific for finnish areacodes (and doesn't take country codes into account) but modify as needed:
// Formats 0451234567 => 045 1234 567
function format_phone($phone) {
// List your area codes here
$phone = preg_replace('/(02|03|05|06|08|09|013|014|015|016|017|018|019|020|040|041|042|043|044|045|046|050)/', '$1 ', $phone);
list($d, $p) = explode(' ', $phone);
$split_point = ceil(strlen($p) / 2);
$p = substr($p, 0, $split_point).' '.substr($p, $split_point);
return $d.' '.$p;
}
For the saving into database part, just strip characters that are non numeric, substr to specific length and insert into db. If your country's phone numbers are all similarly formatted and 9 digits long (without the prefix 0), you can just take the last 9 digits from the phone number using:
$phone = substr($phone, strlen($phone) - 9):
And add 0 to the front. So this effectively turns the country code into 0 but only works if all phone numbers are the same length.