How to convert number to words in PHP - i.e. 1 to 'first'?

Does anybody know of a function / class that will take a numerical input and provide the text positioning of this?

i.e. if I pass '1' to it, I will get the string 'first' returned, '2' will return 'second' etc. etc.
freestateAsked:
Who is Participating?
 
szewkamCommented:
on hurry i wrote really primitive function, it need really good optimization. But it works :)
<?
function getOrdinal($number) {
	$ord1 = array(1 => "first", 2 => "second", 3 => "third", 5 => "fifth", 8 => "eight", 9 => "ninth", 12 => "twelfth");
	$num1 = array("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eightteen", "nineteen");
	$num10 = array("zero", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety");
	$places = array(2 => "hundred", "thousand", 6 => "million", 9 => "billion", 12 => "trillion", 15 => "quadrillion", 18 => "quintillion", 21 => "sextillion", 24 => "septillion", 27 => "octillion");
 
	$number = array_reverse(str_split($number));
	
	if ($number[0] == 0) {
		if ($number[1] >= 2)
			$out = str_replace("y", "ieth", $num10[$number[1]]);
		else
			$out = $num10[$number[1]]."th";
	} else {	
		if (array_key_exists($number[0], $ord1))
			$out = $ord1[$number[0]];
		else
			$out = $num1[$number[0]]."th";
	}	
	
	$i = ($number[0] == 0 ? 2 : 1);
	while ($i < count($number)) {
		if ($i == 1) {
			$out = $num10[$number[$i]] . " " . $out;
			$i++;
		}
		else if ($i == 2) {
			$out = $num1[$number[$i]] . " hundred " . $out;	
			$i++;
		}
		else {
			if (isset($number[$i + 2])) {
				$tmp = $num1[$number[$i + 2]] . " hundred ";
				$tmpnum = $number[$i + 1].$number[$i];
				if ($tmpnum < 20)
					$tmp .= $num1[$tmpnum] . " " . $places[$i] . " ";
				else 
					$tmp .= $num10[$number[$i + 1]] . " " . $num1[$number[$i]] . " " . $places[$i] . " ";
				
				$out = $tmp . $out;
				$i+=3;
			}
			else if (isset($number[$i + 1])) {
				$tmpnum = $number[$i + 1].$number[$i];
				if ($tmpnum < 20)
					$out = $num1[$tmpnum] . " " . $places[$i] . " " . $out;
				else 
					$out = $num10[$number[$i + 1]] . " " . $num1[$number[$i]] . " " . $places[$i] . " " . $out;
				$i+=2;
			}
			else {
				$out = $num1[$number[$i]] . " " . $places[$i] . " " . $out;
				$i++;
			}
		}
	}
 
	return $out;
}
?>

Open in new window

0
 
szewkamCommented:
well, it will be very difficult to generate full word ordinal...
But it could be fairly simple to genereta ordinal suffix for number. Will that satisfy you?
Function taken from: http://www.talkincode.com/create-ordinal-numbers-with-php-382.html
function getOrdinal($number){
 // get first digit
 $digit = abs($number) % 10;
 $ext = 'th';
$ext = ((abs($number) %100 < 21 && abs($number) %100 > 4) ? 'th' : (($digit < 4) ? ($digit < 3) ? ($digit < 2) ? ($digit < 1) ? 'th' : 'st' : 'nd' : 'rd' : 'th'));
 return $number.$ext;
}

Open in new window

0
 
freestateAuthor Commented:
Hi - thanks for the comment, but I've got that happening already (albeit not quite as concisely as your code above, so I'll be revising what I'm using already)

However, what I want is to be able to use the full word as well.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
szewkamCommented:
well... I gave to freestate solution he requested. Maybe this function isn't perfect, but for sure it does the job.
0
 
freestateAuthor Commented:
Sorry folks - a good answer, I've just been a bit busy lately.

Thanks!
0
 
thinknirmalCommented:
The original function of szewkam has problems when rendering numbers between 11 and 19.

I have cleaned up the function if anyone needs it in the future:
function fullWordOrdinal($number) {
    $ord1 = array(1 => "first", 2 => "second", 3 => "third", 5 => "fifth", 8 => "eight", 9 => "ninth", 11 => "eleventh", 12 => "twelfth", 13 => "thirteenth", 14 => "fourteenth", 15 => "fifteenth", 16 => "sixteenth", 17 => "seventeenth", 18 => "eighteenth", 19 => "nineteenth");
    $num1 = array("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eightteen", "nineteen");
    $num10 = array("zero", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety");
    $places = array(2 => "hundred", "thousand", 6 => "million", 9 => "billion", 12 => "trillion", 15 => "quadrillion", 18 => "quintillion", 21 => "sextillion", 24 => "septillion", 27 => "octillion");
    
    $number = array_reverse(str_split($number));
    
    if ($number[0] == 0) {
        if ($number[1] >= 2)
            $out = str_replace("y", "ieth", $num10[$number[1]]);
        else
            $out = $num10[$number[1]]."th";
    } else if ($number[1] == 1) {
        $out = $ord1[$number[1] . $number[0]];
    } else {	
        if (array_key_exists($number[0], $ord1))
            $out = $ord1[$number[0]];
        else
            $out = $num1[$number[0]]."th";
    }
        
    if($number[0] == 0 || $number[1] == 1){
        $i = 2;
    } else {
        $i = 1;
    }
    
    while ($i < count($number)) {
        if ($i == 1) {
            $out = $num10[$number[$i]] . " " . $out;
            $i++;
        } else if ($i == 2) {
            $out = $num1[$number[$i]] . " hundred " . $out;	
            $i++;
        } else {
            if (isset($number[$i + 2])) {
                $tmp = $num1[$number[$i + 2]] . " hundred ";
                $tmpnum = $number[$i + 1].$number[$i];
                if ($tmpnum < 20)
                    $tmp .= $num1[$tmpnum] . " " . $places[$i] . " ";
                else 
                    $tmp .= $num10[$number[$i + 1]] . " " . $num1[$number[$i]] . " " . $places[$i] . " ";
                
                $out = $tmp . $out;
                $i+=3;
            } else if (isset($number[$i + 1])) {
                $tmpnum = $number[$i + 1].$number[$i];
                if ($tmpnum < 20)
                    $out = $num1[$tmpnum] . " " . $places[$i] . " " . $out;
                else 
                    $out = $num10[$number[$i + 1]] . " " . $num1[$number[$i]] . " " . $places[$i] . " " . $out;
                $i+=2;
            } else {
                $out = $num1[$number[$i]] . " " . $places[$i] . " " . $out;
                $i++;
            }
        }
    }
    return $out;
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.