Skip to content

Instantly share code, notes, and snippets.

@inxilpro
Created October 7, 2021 14:52
Show Gist options
  • Save inxilpro/4dfc33c2bf58057b840d60b8ede0eeee to your computer and use it in GitHub Desktop.
Save inxilpro/4dfc33c2bf58057b840d60b8ede0eeee to your computer and use it in GitHub Desktop.
<?php
Str::macro('spellOutNumbers', static function($value, $locale = 'en_us') {
$separatorFormatter = new \NumberFormatter($locale, \NumberFormatter::DECIMAL);
$groupingSeparator = preg_quote($separatorFormatter->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL), '/');
$decimalSeparator = preg_quote($separatorFormatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL), '/');
$spellOutFormatter = new \NumberFormatter($locale, \NumberFormatter::SPELLOUT);
return preg_replace_callback(
"/((?:\d+$groupingSeparator)*\d+(?:$decimalSeparator\d+)?)/",
static fn($match) => $spellOutFormatter->format(preg_replace("/$groupingSeparator/", '', $match[0])),
$value
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment