在網上看到一個非常有趣的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;
}
?>
使用方法:直接傳遞數字參數即可,下面的實例打印出來便是數字大寫。輸出:壹萬貳仟壹佰貳拾壹圓
復制代碼 代碼如下:
<?php
echo cny('12121');
?>