feat(helper): membuat helper untuk format nomor telepon

This commit is contained in:
Yoga Pangestu 2025-10-29 10:03:45 +07:00
parent 3c830259d8
commit 091f34b89b

View File

@ -72,3 +72,20 @@ function sanitizeIntegers(array $data, array $integerKeys = []): array
return $data;
}
}
if (! function_exists('formatPhoneNumber')) {
function formatPhoneNumber(string $number, string $prefix = '+62', bool $useDash = false): string
{
$clean = preg_replace('/\D+/', '', $number);
$clean = preg_replace('/^(0|62)/', '', $clean);
$formatted = $prefix.$clean;
if ($useDash) {
$formatted = preg_replace('/(\d{3})(\d{3,4})(\d{3,4})(\d+)?/', $prefix.'-$1-$2-$3$4', $formatted);
}
return $formatted;
}
}