Number to text converting PHP class
One thing that gets asked quite a bit on forums is how to convert a number into words in PHP, so I thought I’d write a small class that can do this

Here is the code for the class
PHP Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | class num2text { private $_original = 0; private $_parsed_number_text = ''; private $_single_nums = array(1 => 'One', 2 => 'Two', 3 => 'Three', 4 => 'Four', 5 => 'Five', 6 => 'Six', 7 => 'Seven', 8 => 'Eight', 9 => 'Nine', ); private $_teen_nums = array(0 => 'Ten', 1 => 'Eleven', 2 => 'Twelve', 3 => 'Thirteen', 4 => 'Fourteen', 5 => 'Fifteen', 6 => 'Sixteen', 7 => 'Seventeen', 8 => 'Eighteen', 9 => 'Nineteen', ); private $_tens_nums = array(2 => 'Twenty', 3 => 'Thirty', 4 => 'Forty', 5 => 'Fifty', 6 => 'Sixty', 7 => 'Seventy', 8 => 'Eighty', 9 => 'Ninety', ); private $_chunks_nums = array(1 => 'Thousand', 2 => 'Million', 3 => 'Billion', 4 => 'Trillion', 5 => 'Quadrillion', 6 => 'Quintrillion', 7 => 'Sextillion', 8 => 'Septillion', 9 => 'Octillion', 10 => 'Nonillion', 11 => 'Decillion', ); function __construct($number) { $this->_original = trim($number); $this->parse(); } public function parse($new_number = NULL) { if($new_number !== NULL) { $this->_original = trim($new_number); } if($this->_original == 0) return 'Zero'; $num = str_split($this->_original, 1); krsort($num); $chunks = array_chunk($num, 3); krsort($chunks); $final_num = array(); foreach ($chunks as $k => $v) { ksort($v); $temp = trim($this->_parse_num(implode('', $v))); if($temp != '') { $final_num[$k] = $temp; if (isset($this->_chunks_nums[$k]) && $this->_chunks_nums[$k] != '') { $final_num[$k] .= ' '.$this->_chunks_nums[$k]; } } } $this->_parsed_number_text = implode(', ', $final_num); return $this->_parsed_number_text; } public function __toString() { return $this->_parsed_number_text; } private function _parse_num($num) { $temp = array(); if (isset($num[2])) { if (isset($this->_single_nums[$num[2]])) { $temp['h'] = $this->_single_nums[$num[2]].' Hundred'; } } if (isset($num[1])) { if ($num[1] == 1) { $temp['t'] = $this->_teen_nums[$num[0]]; } else { if (isset($this->_tens_nums[$num[1]])) { $temp['t'] = $this->_tens_nums[$num[1]]; } } } if (!isset($num[1]) || $num[1] != 1) { if (isset($this->_single_nums[$num[0]])) { if (isset($temp['t'])) { $temp['t'] .= ' '.$this->_single_nums[$num[0]]; } else { $temp['u'] = $this->_single_nums[$num[0]]; } } } return implode(' and ', $temp); } } |
Using this class is as simple as using two lines of code such as
PHP Code
1 2 | $n2t = new num2text('123456'); echo $n2t; |
Should you wish to parse more than one string after this, you can use
PHP Code
1 | echo $n2t->parse('87654321'); |
It really is that simple to use
Please be aware that in order for all numbers to work, you must enter them as strings
December 15th, 2009 at 1:47 am
What is the point of making this into a class? Shouldn’t it be a standalone function?
December 15th, 2009 at 2:12 am
Basically to encapsulate all the different functions into one object. I was also just getting practice in creating a class as I was bored :)
January 14th, 2010 at 2:22 pm
No need to justify making this a class. I think it’s an excellent piece of code, and hey, it’s what lead me to this site.
Now, your mission Jay, should you choose to accept it, is to make this class reversable. It’s all very well sending it “12″ and it returning “twelve”, but what if I have a “twelve” and want it to return “12″?
Ive written a function that handles this upto 20, but a graceful piece of code should be able to handle anything.
After nearly a month of looking (and trying myself), I haven’t found anything like this.
If you can do it Jay, there’s beer in it for you!