learner
learner

Reputation: 2129

How to convert an integer value to word format using php5?

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

Upvotes: 3

Views: 2943

Answers (4)

No Results Found
No Results Found

Reputation: 102745

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.

Upvotes: 1

itsols
itsols

Reputation: 5582

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.

Upvotes: 1

beerwin
beerwin

Reputation: 10327

Check this in another StackOverflow thread:

Convert a number to its string representation

Upvotes: 2

Kakawait
Kakawait

Reputation: 4029

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

Upvotes: 1

Related Questions