Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3361330
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:08:23+00:00 2026-05-18T03:08:23+00:00

Possible Duplicate: Converting words to numbers in PHP Is there a php function to

  • 0

Possible Duplicate:
Converting words to numbers in PHP

Is there a php function to convert a string such as ‘ten’ into an integer?
If not then how would you go about it?
Have tried google and php.net search with no success

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T03:08:23+00:00Added an answer on May 18, 2026 at 3:08 am

    Here’s a proof of concept class that I just wrote to do this… Check it out:

    class WordToNumber {
    
        public $modifiers = array(
            'hundred' => 100,
        );
    
        public $negatives = array(
            'minus' => true,
            'negative' => true,
        );
    
        public $numbers = array(
            'zero'  => 0,
            'one'   => 1,
            'two'   => 2,
            'three' => 3,
            'four'  => 4,
            'five'  => 5,
            'six'   => 6,
            'seven' => 7,
            'eight' => 8,
            'nine'  => 9,
            'ten'   => 10,
            'eleven' => 11,
            'twelve' => 12,
            'thirteen' => 13,
            'fourteen' => 14,
            'fifteen'  => 15,
            'sixteen'  => 16,
            'seventeen' => 17,
            'eighteen'  => 18,
            'nineteen'  => 19,
            'twenty'    => 20,
            'thirty'    => 30,
            'forty'    => 40,
            'fifty'     => 50,
            'sixty'     => 60,
            'seventy'   => 70,
            'eighty'    => 80,
            'ninety'    => 90,    
        );
    
        public $powers = array(
            'thousand' => 1000,
            'million'  => 1000000,
            'billion'  => 1000000000,
        );
    
        public function __construct() {
        }
    
        public function parse($string) {
            $string = $this->prepare($string);
            $parts = preg_split('#\s+#', $string, -1, PREG_SPLIT_NO_EMPTY);
            $buffer = 0;
            $lastPower = 1;
            $powers = array(
                1 => 0,
            );
            $isNegative = false;
            foreach ($parts as $part) {
                if (isset($this->negatives[$part])) {
                    $isNegative = true;
                } elseif (isset($this->numbers[$part])) {
                    $buffer += $this->numbers[$part];
                } elseif (isset($this->modifiers[$part])) {
                    $buffer *= $this->modifiers[$part];
                } elseif (isset($this->powers[$part])) {
                    if ($buffer == 0) {
                        //Modify last power
                        $buffer = $powers[$lastPower];
                        unset($powers[$lastPower]);
                        $power = $lastPower * $this->powers[$part];
                        $powers[$power] = $buffer;
                        $lastPower = $power;
                        $buffer = 0;
                    } else {
                        $powers[$this->powers[$part]] = $buffer;
                        $buffer = 0;
                        $lastPower = $this->powers[$part];
                    }
                } else {
                    throw new LogicException('Unknown Token Found: '.$part);
                }
            }
            if (!empty($buffer)) {
                $powers[1] = $buffer;
            }
            $total = 0;
            foreach ($powers as $power => $sub) {
                $total += $power * $sub;
            }
            if ($isNegative) {
                $total *= -1;
            }
            return $total;
        }
    
        protected function prepare($string) {
            $string = preg_replace('#(\s+|-|\band\b)#i', ' ', $string);
            $string = mb_convert_case($string, MB_CASE_LOWER);
            return $string;
        }
    
    }
    

    And a test:

    $parser = new WordToNumber();
    
    $strings = array(
        'one hundred and fifty two',
        'fifteen',
        'one thousand million',
        'four hundred thousand five hundred fourty three',
        'fifteen hundred',
        'one thousand twelve hundred',
        'negative two',
        'minus three hundred and fifty seven thousand four hundred and two',
    );
    
    foreach ($strings as $str) {
        echo $parser->parse($str).' - '.$str."\n";
    }
    

    And the results:

    152 - one hundred and fifty two
    15 - fifteen
    1000000000 - one thousand million
    400543 - four hundred thousand five hundred fourty three
    1500 - fifteen hundred
    2200 - one thousand twelve hundred
    -2 - negative two
    -357402 - minus three hundred and fifty seven thousand four hundred and two
    

    It doesn’t support huge numbers (hence why billion is the largest power), but it should be trivially modified to support them by using the bcmath extension… If you want, I could quickly modify it to work with numbers as high as you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Converting Raw HTTP Request into HTTPWebRequest Object I've got a custom HTTP
Possible Duplicate: Converting a string to an enumeration value in C# How do I
Possible Duplicate: Converting string in C# I want to camelize a string, for example:
Possible Duplicate: how can I convert String to Int ? Hi, I have the
Possible Duplicate: converting multline string to array my $text = a=10|b=20|c=20|d=\' I am multing
Possible Duplicate: Parse string into argv/argc I'm trying to write a fake shell using
Possible Duplicate: Converting XML to JSON using Python? I am importing an XML feed
Possible Duplicate: Converting from Mercurial to Subversion Hi. Does anyone know of a relatively
Possible Duplicate: Converting XML to JSON using Python? I'm doing some work on App
Possible Duplicate: Converting a Uniform Distribution to a Normal Distribution Hello. I'd like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.