PHP Classes

PHP Convert Words to Numbers: Convert text with an amount to its numeric value

Recommend this page to a friend!
  Info   View files Example   Screenshots Screenshots   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog (1)    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 60%Total: 284 All time: 7,537 This week: 404Up
Version License PHP version Categories
cwordstonumbers 1.2BSD License5PHP 5, Text processing, Math, Parsers
Description 

Author

This class can can convert text with an amount to its numeric value.

It takes a text string with a number or money amount in English and converts it to the respective number.

The class can automatically detect the numbers using native English or Indian alternatives, like billion or crore.

It returns either the number as integer or formatted string according to the detected number system.

It can deal with words entered in different order, or add values of expressions that represent sums of the numbers like "twelve hundred and twelve hundred and twenty three hundred".

Innovation Award
PHP Programming Innovation award nominee
December 2016
Number 2


Prize: One copy of DWebPro Standard License
Very often applications need to be able to spell numbers using words in a given language. There are many solutions for that because it is a relatively easy problem.

However, there are not many solutions that do the opposite, i.e. take text written in a given language that represents a certain amount and return the number value of the amount that.

This class does precisely that. It takes a text representing an amount in English and returns the number that is spelled.

It is pretty smart to the point of understanding the different ordering that an amount can be spelled as well when the amount is spelled using Indian word variants like lakh and crore instead of millions and billions.

Manuel Lemos
Picture of Mohammed Asad
  Performance   Level  
Name: Mohammed Asad <contact>
Classes: 2 packages by
Country: India India
Age: 34
All time rank: 3422226 in India India
Week rank: 569 Up48 in India India Up
Innovation award
Innovation award
Nominee: 2x

Example

<?php
   
/**
    * A simple example to use this class
    * All you need to do is to include the class and use it right away as given below
    * In other terms, just do ctrl+c and ctrl+v.
    */
require '../src/cWordsToNumbers.class.php';

   
$objWordsToNumbers = new cWordsToNumbers();
$objWordsToNumbers->setEnableNumberFormatting( true );

echo
$objWordsToNumbers->displayNumbers( "nine" ) . "<br/>";
echo
$objWordsToNumbers->displayNumbers( "ninty nine" ) . "<br/>";
echo
$objWordsToNumbers->displayNumbers( "nine hundred and ninty nine" ) . "<br/>";
echo
$objWordsToNumbers->displayNumbers( "nine thousand nine hundred and ninty nine" ) . "<br/>";
echo
$objWordsToNumbers->displayNumbers( "ninty thousand nine hundred and ninty nine" ) . "<br/>";
echo
"<h2>Indian Number system</h2>";

echo
$objWordsToNumbers->displayNumbers( "nine lakh nine thousand nine hundred and ninty nine" ) . "<br/>";

echo
$objWordsToNumbers->displayNumbers( "nine crore nine thousand nine hundred and ninty nine" ) . "<br/>";

echo
"<h2>Western Number system</h2>";

echo
$objWordsToNumbers->displayNumbers( "nine hundred and ninty nine thousand nine hundred and ninty nine" ) . "<br/>";

echo
$objWordsToNumbers->displayNumbers( "nine million nine hundred and ninty nine thousand nine hundred and ninty nine" ) . "<br/>";
?>


Screenshots  
  • Indian_Conversion
  • Western_Conversion
  Files folder image Files  
File Role Description
Files folder imagecwordstonumbers (2 files, 3 directories)

  Files folder image Files  /  cwordstonumbers  
File Role Description
Files folder imageconfig (1 file)
Files folder imagesrc (1 file)
Files folder imagetests (2 files)
  Accessible without login Plain text file LICENSE Lic. License text
  Accessible without login Plain text file README.txt Doc. Documentation

  Files folder image Files  /  cwordstonumbers  /  config  
File Role Description
  Accessible without login Plain text file words_to_numbers_mapper.json Data Auxiliary data

  Files folder image Files  /  cwordstonumbers  /  src  
File Role Description
  Plain text file cWordsToNumbers.class.php Class Class source

  Files folder image Files  /  cwordstonumbers  /  tests  
File Role Description
  Accessible without login Plain text file simpleConversion.php Example Example script
  Accessible without login Plain text file simpleWebFormConversion.php Example Example script

 Version Control Unique User Downloads Download Rankings  
 75%
Total:284
This week:0
All time:7,537
This week:404Up
User Ratings User Comments (1)
 All time
Utility:75%StarStarStarStar
Consistency:75%StarStarStarStar
Documentation:75%StarStarStarStar
Examples:75%StarStarStarStar
Tests:-
Videos:-
Overall:60%StarStarStarStar
Rank:1365