Php converts Chinese characters to numbers,
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.
Articles you may be interested in:
- Php chinese letter/digit Verification Code
- Php implements a function for converting Arabic numerals to Chinese numerals
- Supports php verification code for Chinese letters, numbers, and custom Fonts
- PHP function code for converting RMB into uppercase letters
- Php determines whether it is a sample code of Chinese/English/numbers
- Php + Regular Expression separates letters and numbers from Chinese Characters
- PHP verification code that supports Chinese characters, letters, and numbers
- Php implements filtering of Chinese characters and numbers in strings