tags:

views:

969

answers:

5

In PHP, is there an easy way to convert a number to a word? For instance, 27 to twenty-seven.

+1  A: 

There's no built-in way that I'm aware of, but if you have a look at my example code for this question (in C#) it should be easy enough for you to get working. Basically it'd just be a matter of untyping the variables and adding the $ prefix where appropriate and it should work.

Matthew Scharley
+1  A: 

I found two links for this. The first is a freeware PHP class you could use, the second is a example function you could have a look at and copy or use to write your own.

schnaader
+3  A: 

http://bloople.net/num2text/ includes source code.

"It's copyright, but you can use it freely and modify it however you want."

mrhahn
+3  A: 

There is a package in PECL.

Milen A. Radev
+1  A: 

Alternatively, you can use the NumberFormatter class from 'intls' package in PHP . Here's a sample code to get you started (for commandline):

    <?php
if ($argc < 3) 
    {
    echo "usage: php {$argv[0]} lang-tag number ...\n";
    exit;
    }

array_shift($argv);
$lang_tag = array_shift($argv);

$nf1 = new NumberFormatter($lang_tag, NumberFormatter::DECIMAL);
$nf2 = new NumberFormatter($lang_tag, NumberFormatter::SPELLOUT);

foreach ($argv as $num) 
    {
    echo $nf1->format($num).' is '.$nf2->format($num)."\n"; 
    }
?>