首页> 实战笔录 >PHP开发笔记 >PHP PHP

php将汉字大写金额转化成数字方法分享

作者:小萝卜 2024-07-19 浏览 575

简介php将汉字大写金额转化成数字方法分享,例如:叁万零陆佰柒拾元零玖分转化成30670.09

php将汉字大写金额转化成数字方法分享,例如:叁万零陆佰柒拾元零玖分转化成30670.09

代码分享:

if(!function_exists('cn2num')) {
    /**
     * 将汉字大写金额转化为数字
     * @param $upperCaseNumber
     * @return array|string|string[]|null
     */
    function cn2num($string){
        if(is_numeric($string)){
            return $string;
        }
        // '仟' => '千','佰' => '百','拾' => '十',
        $string = str_replace('仟', '千', $string);
        $string = str_replace('佰', '百', $string);
        $string = str_replace('拾', '十', $string);
        $num = 0;
        $wan = explode('万', $string);
        if (count($wan) > 1) {
            $num += cn2num($wan[0]) * 10000;
            $string = $wan[1];
        }
        $qian = explode('千', $string);
        if (count($qian) > 1) {
            $num += cn2num($qian[0]) * 1000;
            $string = $qian[1];
        }
        $bai = explode('百', $string);
        if (count($bai) > 1) {
            $num += cn2num($bai[0]) * 100;
            $string = $bai[1];
        }
        $shi = explode('十', $string);
        if (count($shi) > 1) {
            $num += cn2num($shi[0] ? $shi[0] : '一') * 10;
            $string = $shi[1] ? $shi[1] : '零';
        }
        $ling = explode('零', $string);
        if (count($ling) > 1) {
            $string = $ling[1];
        }
        $d = array(
            '一' => '1','二' => '2','三' => '3','四' => '4','五' => '5','六' => '6','七' => '7','八' => '8','九' => '9',
            '壹' => '1','贰' => '2','叁' => '3','肆' => '4','伍' => '5','陆' => '6','柒' => '7','捌' => '8','玖' => '9',
            '零' => 0, '0' => 0, 'O' => 0, 'o' => 0,
            '两' => 2
        );
        return $num + @$d[$string];
    }
}
 

很赞哦! (5)

文章评论

    高端网站建设