Created
October 7, 2021 14:52
-
-
Save inxilpro/4dfc33c2bf58057b840d60b8ede0eeee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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