本文 |
function knum2num($knum) { // 漢数字 $kltr = ['〇', '一', '二', '三', '四', '五', '六', '七', '八', '九']; // 桁(ベースの4桁、上位桁中でも繰り返される) $kdig = [ '十' => 10, '百' => 100, '千' => 1000, ]; // 上位桁 $kdig_u = [ '万' => 10000, '億' => 100000000, '兆' => 1000000000000, '京' => 10000000000000000, '垓' => 100000000000000000000, ]; // $kvmap = array_flip($kltr);
$len = mb_strlen($knum); $dig = 1; $dig_u = 1; $dig_chg = false; $val = '0'; for ($i = $len; $i-- > 0;) { $c = mb_substr($knum, $i, 1); if (isset($kvmap[$c])) { $val = bcadd($val, bcmul($kvmap[$c], bcmul($dig, $dig_u, 0), 0), 0); $dig = bcmul($dig, 10, 0); $dig_chg = false; } else if (isset($kdig[$c])) { if (!$dig_chg) { $dig_chg = true; } else { $val = bcadd($val, bcmul($dig, $dig_u, 0), 0); } $dig = $kdig[$c]; } else if (isset($kdig_u[$c])) { $dig_u = $kdig_u[$c]; $dig = 1; $dig_chg = false; } } if ($dig_chg) { $val = bcadd($val, bcmul($dig, $dig_u, 0), 0); }
return $val; }
|