3

I want to convert a number to word format.

For example:

$count = 5;
echo "Hello Mr user this is your ".$count." review."

I need output like this...

"Hello Mr user this is your fifth review."

hakre
  • 193,403
  • 52
  • 435
  • 836
learner
  • 2,099
  • 6
  • 23
  • 32
  • 1
    possible duplicate of [Is there an easy way to convert a number to a word in PHP?](http://stackoverflow.com/questions/277569/is-there-an-easy-way-to-convert-a-number-to-a-word-in-php) – Shakti Singh May 18 '11 at 06:22
  • Check it out http://www.php.net/manual/en/function.number-format.php#97020 – Shakti Singh May 18 '11 at 06:24
  • 1
    @Shakti: Neither of these links ordinalize the number as OP wants (1st, 3rd, 5th - First, Third, Fifth), but they are useful still. This is not an exact duplicate. – Wesley Murch May 18 '11 at 06:26
  • @Wesley Murch (+1), good observation. I just have an extra task for my open source converter :) – itsols May 18 '11 at 06:32
  • how high do you want these numbers to go? I mean what's the general range? – itsols May 18 '11 at 06:33

4 Answers4

2

Check this in another StackOverflow thread:

Convert a number to its string representation

Community
  • 1
  • 1
beerwin
  • 9,813
  • 6
  • 42
  • 57
1

There is no built in function in php to make that. But try external lib like : http://pear.php.net/package/Numbers_Words

Kakawait
  • 3,929
  • 6
  • 33
  • 60
1

If you're referring to a built-in function, no, PHP does not have one. But You can create your own.

If you're looking at only English numbers, it's not much of an issue. But if you need to deal with a foreign language (like Arabic), you have to do a bit of extra work since numbers and objects have genders. So the algorithm gets a little more complex.

Just for the record, I'm working on producing an open source conversion tool for Arabic.

itsols
  • 5,406
  • 7
  • 51
  • 95
1

I know this isn't exactly what you're after, but I picked this up from the comments on php.net somewhere (can't find source), it works for output like 1st, 243rd and 85th.

function ordinalize($num)
{
    if ( ! is_numeric($num)) return $num;

    if ($num % 100 >= 11 and $num % 100 <= 13)
    {
        return $num."th";
    }
    elseif ( $num % 10 == 1 )
    {
        return $num."st";
    }
    elseif ( $num % 10 == 2 )
    {
        return $num."nd";
    }
    elseif ( $num % 10 == 3 )
    {
        return $num."rd";
    }
    else
    {
        return $num."th";
    }
}

You might want to even consider this format anyways for readability and simplicity, depending on how high you expect the numbers to be. If they are less than 100, you should be able to write your own function easily. However, if they can get really high:

"Hello Mr user this is your three thousand four hundred and twenty-fifth review."

Sounds a bit awkward :)

Hope this helps some.

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228