Here we will share with you a php code for converting Chinese characters to numbers. it is very intelligent and perfect. if you need it, you can refer to it. Share an auxiliary function and use php to identify numbers in strings as much as possible,
Code First
Function checkNatInt ($ str) {$ map = array ('1' => '1', '2' => '2', '3' => '3 ', '4' => '4', '5' => '5', '6' => '6', '7' => '7 ', '8' => '8', '9' => '9', '1' => '1', 'ER' => '2 ', 'san' => '3', 'ta' => '4', 'wu' => '5', 'loan' => '6 ', 'hangzhou' => '7', 'hangzhou' => '8', 'hangzhou' => '9', '0' => '0 ', 'two' => '2', 'clerk' => 'kil', 'clerk' => 'bai', 'Start' => '10 ', 'ten thousand '=> 'Hundred million',); $ str = str_replace (array_keys ($ map), array_values ($ map), $ str); $ str = checkString ($ s Tr, '/([\ d hundreds of millions] +)/u'); $ func_c2i = function ($ str, $ plus = false) use (& $ func_c2i) {if (false = $ plus) {$ plus = array ('100 '=> 100000000, 'wan' => 10000, 'kilobytes => 1000, '100' => 100, '10' => 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: function 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 result is as follows.
Echo checkNatInt ('20140901'); // common Chinese numbers, 9.6 million echo checkNatInt ('one hundred millions of Chinese characters can be found anywhere '); // uppercase Chinese numbers, 170000840 echo checkNatInt ('earn one million annual salary for PHP learn'); // number in the sentence, 1000000 echo checkNatInt ('send 2.6 billion million people this spring festival '); // numbers and Chinese characters are mixed, 2600000000 echo checkNatInt ('Two beebot '); // colloquial number, 2 echo checkNatInt ('Never imagined '); // retro expression, 100000000
Notes
Decimal places not supported
PHP_INT_MAX is not supported.