- test();
- /**
- * Test
- */
- function test() {
- echo CnToInt('一'); // 1
- echo CnToInt('十'); // 10
- echo CnToInt ('Eleven'); // 11
- echo CnToInt('One hundred and ten'); // 110
- echo CnToInt('One thousand and one'); // 1001
- echo CnToInt('One thousand and one hundred' Zero one'); // 10101
- echo CnToInt('one hundred and thirteen million and three thousand and one'); // 113003001
- echo CnToInt('one quadrillion'); // 11.0E+15
- }
-
- /**
- * Chinese to number
- * @param String $var Chinese number to be parsed
- * @param Int $start initial value
- * @return int
- */
- function CnToInt($var, $start = 0) {
- if (is_numeric($var)) {
- return $var;
- }
- if (intval($var) = == 0) {
- $splits = array('100 million' => 100000000, '10,000' => 10000);
- $chars = array('10,000' => 10000, '1000' => 1000, 'hundred' => 100, '十' => 10, '一' => 1, 'zero' => 0);
- $Ints = array('zero' => 0, '一' => 1, '二' => 2, '三' => 3, '四' => 4, '五' => 5, '六' => 6, '七' => ; 7, '八' => 8, '九' => 9, '十' => 10);
- $var = str_replace('zero', "", $var);
- foreach ($splits as $key => $step) {
- if (strpos($var, $key)) {
- $strs = explode($key, $var);
- $start += CnToInt(array_shift($strs)) * $step;
- $var = join('', $strs);
- }
- }
- foreach ($chars as $key => $step) {
- if (strpos($var, $key) !== FALSE ) {
- $vs = explode($key, $var);
- if ($vs[0] === "") {
- $vs[0] = '一';
- }
- $start += $Ints [array_shift($vs)] * $step;
- $var = join('', $vs);
- } elseif (mb_strlen($var, 'utf-8') === 1) {
- $start += $Ints[$var];
- $var = '';
- break;
- }
- }
- return $start;
- } else {
- return intval($var);
- }
- }
Copy code
|