|
deep inside webpages
 


Number-to-Word Conversion with PHP

This function accepts integer or string representation of integer and converts to English words. This can be helpful for your online shopping carts, and for generating invoices online.


  1. function number_to_word( $num = '' )
  2. {
  3.     $num    = ( string ) ( ( int ) $num );
  4.    
  5.     if( ( int ) ( $num ) && ctype_digit( $num ) )
  6.     {
  7.         $words  = array( );
  8.        
  9.         $num    = str_replace( array( ',' , ' ' ) , '' , trim( $num ) );
  10.        
  11.         $list1  = array('','one','two','three','four','five','six','seven',
  12.             'eight','nine','ten','eleven','twelve','thirteen','fourteen',
  13.             'fifteen','sixteen','seventeen','eighteen','nineteen');
  14.        
  15.         $list2  = array('','ten','twenty','thirty','forty','fifty','sixty',
  16.             'seventy','eighty','ninety','hundred');
  17.        
  18.         $list3  = array('','thousand','million','billion','trillion',
  19.             'quadrillion','quintillion','sextillion','septillion',
  20.             'octillion','nonillion','decillion','undecillion',
  21.             'duodecillion','tredecillion','quattuordecillion',
  22.             'quindecillion','sexdecillion','septendecillion',
  23.             'octodecillion','novemdecillion','vigintillion');
  24.        
  25.         $num_length = strlen( $num );
  26.         $levels = ( int ) ( ( $num_length + 2 ) / 3 );
  27.         $max_length = $levels * 3;
  28.         $num    = substr( '00'.$num , -$max_length );
  29.         $num_levels = str_split( $num , 3 );
  30.        
  31.         foreach( $num_levels as $num_part )
  32.         {
  33.             $levels--;
  34.             $hundreds   = ( int ) ( $num_part / 100 );
  35.             $hundreds   = ( $hundreds ? ' ' . $list1[$hundreds] . ' Hundred' . ( $hundreds == 1 ? '' : 's' ) . ' ' : '' );
  36.             $tens       = ( int ) ( $num_part % 100 );
  37.             $singles    = '';
  38.            
  39.             if( $tens < 20 )
  40.             {
  41.                 $tens   = ( $tens ? ' ' . $list1[$tens] . ' ' : '' );
  42.             }
  43.             else
  44.             {
  45.                 $tens   = ( int ) ( $tens / 10 );
  46.                 $tens   = ' ' . $list2[$tens] . ' ';
  47.                 $singles    = ( int ) ( $num_part % 10 );
  48.                 $singles    = ' ' . $list1[$singles] . ' ';
  49.             }
  50.             $words[]    = $hundreds . $tens . $singles . ( ( $levels && ( int ) ( $num_part ) ) ? ' ' . $list3[$levels] . ' ' : '' );
  51.         }
  52.        
  53.         $commas = count( $words );
  54.        
  55.         if( $commas > 1 )
  56.         {
  57.             $commas = $commas - 1;
  58.         }
  59.        
  60.         $words  = implode( ', ' , $words );
  61.        
  62.         //Some Finishing Touch
  63.         //Replacing multiples of spaces with one space
  64.         $words  = trim( str_replace( ' ,' , ',' , trim_all( ucwords( $words ) ) ) , ', ' );
  65.         if( $commas )
  66.         {
  67.             $words  = str_replace_last( ',' , ' and' , $words );
  68.         }
  69.        
  70.         return $words;
  71.     }
  72.     else if( ! ( ( int ) $num ) )
  73.     {
  74.         return 'Zero';
  75.     }
  76.     return '';
  77. }
PHP Code

Dependencies

number_to_word()
requires two additional functions
trim_all()
and
str_replace_last()
that can be found here and here.

Sources

  1. Names of Large Numbers collected from this wikipedia page.

Alternatives

There are some other alternatives to this function that produce almost similar output. If you want to have a look, here are these:
  1. PEAR: http://pear.php.net/package/Numbers_Words
  2. PECL: http://php.net/manual/en/numberformatter.format.php

Usage

  1. echo number_to_word( '2281941596' );
  2.  
  3. //Sample output - number_to_word
  4. Two Billion, Two Hundreds Eighty One Million, Nine Hundreds Forty One Thousand and Five Hundreds Ninety Six
  5.  
  6. //Sample output - PEAR Class
  7. two billion two hundred eighty-one million nine hundred forty-one thousand five hundred ninety-six
PHP Code



  1. Replace last occurance of a String - PHP
  2. Remove undesired characters with trim_all() - PHP
  3. How to validate ASCII Text - PHP
  4. register_globals is Back - PHP Implementation
  5. Faster Random Numbers with PHP