10, 'puluh' => 10, 'ratus' => 100, 'ribu' => 1000, 'juta' => 1000000, 'milyar' => 1000000000, 'triliun' => 1000000000000 ); $text = strtolower($text); $words = array_map('trim', explode(' ', $text)); // fix some textual error $keywords = array_merge(array_keys($numbers), array_keys($exponents)); $keypattern = implode('|', $keywords); $temp = array(); foreach($words as $word) { $word = str_replace('rb', 'ribu', $word); $word = str_replace('jt', 'juta', $word); $word = str_replace('rts', 'ratus', $word); $word = str_replace('trilyun', 'triliun', $word); if (substr($word, 0, 2) == 'se' && substr($word, 0, 8) != 'sembilan') { $temp[] = 'satu'; $word = substr($word, 2); } while (preg_match("/^($keypattern)/", $word, $match)) { $word = substr($word, strlen($match[1])); $temp[] = $match[1]; } } $words = $temp; if (empty($words)) { return false; } // begin process $stack = array(); $result = 0; $seen_digits = 0; //echo "processing words [", implode(',', $words), "]
\n"; $numpattern = implode('|', array_keys($numbers)); $exppattern = implode('|', array_keys($exponents)); while($word = array_shift($words)) { //echo "processing word $word
\n"; if (isset($numbers[$word])) { if ($seen_digits) { array_push($stack, (10 * array_pop($stack)) + $numbers[$word]); } else { array_push($stack, $numbers[$word]); $seen_digits = 1; } //echo "found number ", $numbers[$word], ",stack: [", implode(",", $stack), "]
\n"; } else if ($word == 'belas') { array_push($stack, 10 + array_pop($stack)); $seen_digits = 0; //echo "found belasan, stack: [", implode(",", $stack), "]
\n"; } else if (isset($exponents[$word])) { //echo "found exponent: ", $exponents[$word], ", stack: [", implode(",", $stack), "]
\n"; $subtot = 0; while($a = array_pop($stack)) { if ($a <= $exponents[$word]) { $subtot += $a; //echo "calculate total: $subtot, stack: [", implode(",", $stack), "]
\n"; } else { array_push($stack, $a); break; } } array_push($stack, $exponents[$word] * $subtot); $seen_digits = 0 ; //echo "subtot: $subtot, stack: [", implode(",", $stack), "]
\n"; } //echo "
\n"; } while($t = array_shift($stack)) { $result += $t; } return $result; } ?>