php - Convert String 'ten' to Integer 10 -
possible duplicate:
converting words numbers in php
is there php function convert string such 'ten' integer?
if not how go it?
have tried google , php.net search no success
here's proof of concept class wrote this... check 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 $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 $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 test:
$parser = new wordtonumber(); $strings = array( 'one hundred , fifty two', 'fifteen', 'one thousand million', 'four hundred thousand 5 hundred fourty three', 'fifteen hundred', 'one thousand twelve hundred', 'negative two', 'minus 3 hundred , fifty 7 thousand 4 hundred , two', ); foreach ($strings $str) { echo $parser->parse($str).' - '.$str."\n"; }
and results:
152 - 1 hundred , fifty 2 15 - fifteen 1000000000 - 1 thousand million 400543 - 4 hundred thousand 5 hundred fourty 3 1500 - fifteen hundred 2200 - 1 thousand twelve hundred -2 - negative 2 -357402 - minus 3 hundred , fifty 7 thousand 4 hundred , 2
it doesn't support huge numbers (hence why billion largest power), should trivially modified support them using bcmath extension... if want, modify work numbers high want.
Comments
Post a Comment