大写金额转换成阿拉伯数字金额
程序员文章站
2022-06-18 08:50:48
...
<?php
/**
* 大写金额转换成阿拉伯数字金额
*
* FuntionName:convert2Number
* @param string|$moneyCn
* @return int|mixed
*
* $moneyCn = "贰亿零壹拾万叁仟零壹圆"; -> 200103001
* $moneyCn = "玖仟捌佰叁拾亿零壹拾捌万贰仟柒佰圆整"; -> 983000182700
* $moneyCn = "贰佰亿壹仟万零壹圆整"; -> 20010000001
*/
function convert2Number($moneyCn)
{
static $unit2Number = [
'亿' => 100000000,
'万' => 10000,
'仟' => 1000,
'佰' => 100,
'拾' => 10,
'圆' => 1,
];
static $cn2Number = [
'零' => 0,
'壹' => 1,
'贰' => 2,
'叁' => 3,
'肆' => 4,
'伍' => 5,
'陆' => 6,
'柒' => 7,
'捌' => 8,
'玖' => 9,
'拾' => 10,
];
$result = 0;
foreach($unit2Number as $unit => $number) {
$pos = mb_strpos($moneyCn, $unit);
if ($pos === false) {
// 这个就是没有找到这个需要的单位
continue;
}
$cn = mb_substr($moneyCn, 0, $pos);
if (mb_substr($cn, 0, 1) == '零') {
$cn = mb_substr($cn, 1);
}
$number = 0;
if (mb_strlen($cn) == 1) {
$number = $cn2Number[$cn];
} else if (mb_strlen($cn) > 1) {
$number = convert2Number($cn . "圆");
}
$result += $number * $unit2Number[$unit];
$moneyCn = mb_substr($moneyCn, $pos + mb_strlen($unit));
}
return $result;
}
$moneyCn = "玖仟捌佰叁拾亿零壹拾捌万贰仟柒佰圆整";
echo convert2Number($moneyCn);//983000182700