Share an auxiliary function, using PHP to identify as many numbers as possible in the string,
First on the code
function Checknatint ($str) {$map = array (' One ' = ' 1 ', ' two ' = ' 2 ', ' three ' = ' 3 ', ' four ' = ' 4 ', ' five ' = ' 5 ', ' VI ' = > ' 6 ', ' seven ' = ' 7 ', ' eight ' = ' 8 ', ' nine ' = ' 9 ', ' One ' and ' 1 ', ' II ' and ' 2 ', ' three ' = ' 3 ', ' ' ' ' and ' 4 ', ' Wu ' and ' 5 ', ' Land ' and ' 6 ', ' qi ' and ' 7 ', ' ba ' = ' 8 ', ' nine ' = ' 9 ', ' 0 ' = ' 0 ', ' two ' = ' 2 ', ' thousand ' = ' thousand ', ' bai ' and ' hundred ' , ' pick ' up ' = ' ten ', ' absolute ' = ' billion ', '; $str = Str_replace (Array_keys ($map), Array_values ($map), $str); $str = checkstring ($str, '/([\d million Baishi]+)/U '); $func _c2i = function ($str, $plus = False) use (& $func _c2i) {if (false = = = $plus) {$plus = array (' billion ' + 100 000000, ' million ' and 10000, ' thousand ' = 1000, ' hundred ' = 100, ' Ten ' + 10, '; } $i = 0; if ($plus) foreach ($plus as $k = + $v) {$i + +; if (Strpos ($str, $k)!== false) {$ex = explode ($k, $STR, 2); $new _plus = Array_slice ($plus, $i, NULL, TRUE); $l = $func _c2i ($ex [0], $new _plus); $r = $func _c2i ($ex [1], $new _plus); if ($l = = 0) $l = 1; Return $l * $v + $r; }} return (int) $str; } return $func _c2i ($STR); }//From the UCT PHP development Framework, where the checkstring function is as follows functions Checkstring ($var, $check = ", $default =") {if (!is_string ($var)) { if (Is_numeric ($var)) {$var = (string) $var; } else {return $default; }} if ($check) {return (Preg_match ($check, $var, $ret)? $ret [1]: $default); } return $var;}
The implementation results are as follows.
echo checknatint (' 9.6 million '); Ordinary Chinese numerals, 9600000echo checknatint (' Yi billion qi thousand million Ba Bai restaurant pick up '); Uppercase Chinese numerals, 170000840
Precautions
Decimals not supported
Numbers greater than Php_int_max are not supported