PHP function to spell out numbers 1-9 for proper English?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

PHP function to spell out numbers 1-9 for proper English?

Post by JAB Creations »

In proper English the numbers zero through nine must be spelled out while ten and above do not. Is there a native function for this in PHP?

In example...
Zero
One
Two
Three
Four
Five
Six
Seven
Eight
Nine
10
11
12
etc...
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP function to spell out numbers 1-9 for proper English?

Post by requinix »

JAB Creations wrote:In proper English the numbers zero through nine must be spelled out while ten and above do not.
Debatable.
JAB Creations wrote:Is there a native function for this in PHP?
Not that I know of.

Code: Select all

function spell($number) {
    if ($number >= 0 && $number < 10 && (is_int($number) || ctype_digit($number))) {
        $numbers = array("Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine");
        return $numbers[$number];
    } else return $number;
}
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: PHP function to spell out numbers 1-9 for proper English?

Post by JAB Creations »

I'm not interested in debating English grammar. :P

That was a quick reply, did you write that function out that quickly or was it something you've had archived? I appreciate it, thanks! :mrgreen:
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP function to spell out numbers 1-9 for proper English?

Post by requinix »

JAB Creations wrote:That was a quick reply, did you write that function out that quickly or was it something you've had archived?
Just wrote it. Simple enough, only took a minute.
Post Reply