function phone($phone) {
$cleaned = preg_replace('/[^[:digit:]]/', '', $phone);
if (strlen($phone) == 12) {
preg_match('/(\d{2})(\d{3})(\d{3})(\d{2})(\d{2})/', $cleaned, $matches);
return "+{$matches[1]} ({$matches[2]}) {$matches[3]}-{$matches[4]}-{$matches[5]}";
} else {
return $cleaned ;
}
}
$phone = "380501234567";
echo phone($phone);