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: 285 All time: 7,538 This week: 120Up
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: 35
All time rank: 3418227 in India India
Week rank: 312 Up22 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/>";
?>


Details

INTRODUCTION:

This class is created for converting the words( that represents the digits) to numbers again like thousand to 1000, hundred to 100 or so.

The main purpose for creating this class is just to help someone who was looking for this and I also had lot of free time.

CHALLENGES faced to make this work:

-- changing the order of the words and the different probability of words order.

-- Committing spelling mistakes or using extra gaps or unwanted characters.

-- Using indian and western system together (like billion crore). :P

FEATURES:

-- It can automatically detect the entered number system either be indian or western.

-- This can also help to get the digits for terms like twelve hundred or twenty five hundred.

-- You can also find the sum of the numbers by typing them like (eg.twelve hundred and twelve hundred and twenty three hundred),well it know its the hard way, but still you can do it.

-- It can format automatically based on the detected number system if the formatting is enabled.

-- Pretty simple to use, it. Just create and object and call the displayNumbers method.

LIMITATIONS:

-- Indian number system limit is till ninty nine lakh crore and ninty nine lakh and so on. (99,99,999,99,99,999)

-- Western number system limit is till nine hundred and ninty nine billion and so one (999,999,999,999)

-- Using wrong ordered words might create a chaos (I am not responsible if your machine explodes)

-- Currently Supports only ENGLISH LANGUAGE

LANGUAGE SUPPORT:

ONLY ENGLISH


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:285
This week:0
All time:7,538
This week:120Up
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:1220