Home  >  Article  >  php教程  >  PHP人民币金额数字转中文大写的函数代码

PHP人民币金额数字转中文大写的函数代码

WBOY
WBOYOriginal
2016-06-13 11:55:141327browse

在网上看到一个非常有趣的PHP人民币金额数字转中文大写的函数,其实质就是数字转换成中文大写,测试了一下,非常有趣,随便输个数字,就可以将其大写打印出来,新手朋友们试一下吧,举一反三,还可以写好多有趣的PHP转换函数。

复制代码 代码如下:


 
function cny($ns) {
    static $cnums=array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖"),
        $cnyunits=array("圆","角","分"),
        $grees=array("拾","佰","仟","万","拾","佰","仟","亿");
    list($ns1,$ns2)=explode(".",$ns,2);
    $ns2=array_filter(array($ns2[1],$ns2[0]));
    $ret=array_merge($ns2,array(implode("",_cny_map_unit(str_split($ns1),$grees)),""));
    $ret=implode("",array_reverse(_cny_map_unit($ret,$cnyunits)));
    return str_replace(array_keys($cnums),$cnums,$ret);
}





复制代码 代码如下:


function _cny_map_unit($list,$units) {
    $ul=count($units);
    $xs=array();
    foreach (array_reverse($list) as $x) {
        $l=count($xs);
        if ($x!="0" || !($l%4)) $n=($x=='0'?'':$x).($units[($l-1)%$ul]);
        else $n=is_numeric($xs[0][0])?$x:'';
        array_unshift($xs,$n);
    }
    return $xs;
}
?>


使用方法:直接传递数字参数即可,下面的实例打印出来便是数字大写。输出:壹万贰仟壹佰贰拾壹圆

复制代码 代码如下:


 echo cny('12121');
?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn