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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

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.
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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

Thanks!
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.