Created
January 10, 2019 19:01
-
-
Save asipple1/366b55a54dae5f1c06c8aa079f00ff57 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
function convert_number_to_word($num = FALSE) { | |
$num = str_replace([',', ' '], '', trim($num)); | |
if (!$num) { | |
return FALSE; | |
} | |
$num = (int) $num; | |
$words = []; | |
$list1 = [ | |
'', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', | |
'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', | |
]; | |
$list2 = [ | |
'', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety', 'hundred', | |
]; | |
$list3 = [ | |
'', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion', 'decillion', | |
'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion', 'quindecillion', 'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion', | |
]; | |
$num_length = strlen($num); | |
$levels = (int) (($num_length + 2) / 3); | |
$max_length = $levels * 3; | |
$num = substr('00' . $num, -$max_length); | |
$num_levels = str_split($num, 3); | |
for ($i = 0; $i < count($num_levels); $i++) { | |
$levels--; | |
$hundreds = (int) ($num_levels[$i] / 100); | |
$hundreds = ($hundreds ? ' ' . $list1[$hundreds] . ' hundred ' : ''); | |
$tens = (int) ($num_levels[$i] % 100); | |
$singles = ''; | |
if ($tens < 20) { | |
$tens = ($tens ? ' ' . $list1[$tens] . ' ' : ''); | |
} | |
else { | |
$tens = (int) ($tens / 10); | |
$tens = ' ' . $list2[$tens] . ' '; | |
$singles = (int) ($num_levels[$i] % 10); | |
$singles = ' ' . $list1[$singles] . ' '; | |
} | |
$words[] = $hundreds . $tens . $singles . (($levels && (int) ($num_levels[$i])) ? ' ' . $list3[$levels] . ' ' : ''); | |
} //end for loop | |
$commas = count($words); | |
if ($commas > 1) { | |
$commas = $commas - 1; | |
} | |
return trim(implode(' ', $words)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment