一種中文數字轉阿拉伯數字的解決方案

eddy8發表於2021-03-30

網上不少的程式碼都有bug,在此給出一種方案:

// chineseToNumber('二百');

function chineseToNumber($str) {
    if(is_numeric($str)){
        return intval($str);
    }
    $words = preg_split('/(?<!^)(?!$)/u', $str);

    $pos = 0;
    $section = 0;
    $rtn = 0;
    $secUnit = false;
    $number = 0;
    $length = count($words);
    while ($pos < $length) {
        $num = chineseToValue(mb_substr($str, $pos, 1));
        if ($num >= 0) {
            $number = $num;
            $pos += 1;
            if ($pos >= $length) {
                $section += $number;
                $rtn += $section;
                break;
            }
        } else {
            $unit = chineseToUnit(mb_substr($str, $pos, 1), $secUnit);
            if ($secUnit) {
                $section = ($section + $number) * $unit;
                $rtn += $section;
                $section = 0;
            } else {
                $section += ($number * $unit);
            }

            $number = 0;
            $pos += 1;
            if($pos >= $length) {
                $rtn += $section;
                break;
            }
        }
    }
    return $rtn;
}

function chineseToUnit($str, &$secUnit)
{
    $data = [
        ["十", 10, false],
        ["百", 100, false],
        ["千", 1000, false],
        ["萬", 10000, true],
        ["億", 100000000, true],
    ];
    foreach ($data as $v) {
        if ($v[0] === $str) {
            $secUnit = $v[2];
            return $v[1];
            break;
        }
    }

    return null;
}

function chineseToValue($str)
{
    $d = array(
        '一' => '1','二' => '2','三' => '3','四' => '4','五' => '5','六' => '6','七' => '7','八' => '8','九' => '9',
        '壹' => '1','貳' => '2','叄' => '3','肆' => '4','伍' => '5','陸' => '6','柒' => '7','捌' => '8','玖' => '9',
        '零' => 0,
        '兩' => 2
    );
    foreach ($d as $k => $v) {
        if ($k === $str) {
            return $v;
        }
    }

    return -1;
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章