Skip to content

Instantly share code, notes, and snippets.

@bobbydank
Last active July 27, 2022 02:54
Show Gist options
  • Save bobbydank/76b0c4d26f2b47393d645984b74bd951 to your computer and use it in GitHub Desktop.
Save bobbydank/76b0c4d26f2b47393d645984b74bd951 to your computer and use it in GitHub Desktop.
Some useful php functions for receiving and returning various values from various inputs. Going from numbers to words and visa versa.
<?php
/*
* This function turns a number into the ordinal word for the number
*
* Parameter : integer
* Returns : string
*/
function numToOrdinalWord($num) {
$first_word = array('eth','first','second','third','fourth','fifth','sixth','seventh','eighth','ninth','tenth','eleventh','twelfth','thirteenth','fourteenth','fifteenth','sixteenth','seventeenth','eighteenth','nineteenth','twentieth');
$second_word = array('','','twenty','thirty','forty','fifty', 'sixty', 'seventy', 'eighty', 'ninty');
if($num <= 20) {
return $first_word[$num];
}
$first_num = substr($num,-1,1);
$second_num = substr($num,-2,1);
return $string = str_replace('y-eth','ieth',$second_word[$second_num].'-'.$first_word[$first_num]);
}
/*
* This function turns a number into the ordinal prefix for the number
*
* Parameter : integer
* Returns : string
*/
function numToOrdinalNumber($number) {
$ends = array('th','st','nd','rd','th','th','th','th','th','th');
if ((($number % 100) >= 11) && (($number%100) <= 13)) {
return $number. 'th';
} else {
return $number. $ends[$number % 10];
}
}
/*
* This function turns a number into the word(s) for the number
*
* Parameter : integer
* Returns : string
*/
function convertNumberToWord($num = false) {
$num = str_replace(array(',', ' '), '' , trim($num));
if(! $num) {
return false;
}
$num = (int) $num;
$words = array();
$list1 = array('', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
);
$list2 = array('', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety', 'hundred');
$list3 = array('', '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 implode(' ', $words);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment