Skip to content

Instantly share code, notes, and snippets.

@drunkday
Created October 11, 2012 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drunkday/3869919 to your computer and use it in GitHub Desktop.
Save drunkday/3869919 to your computer and use it in GitHub Desktop.
中文数字转化大写
<?php
function trans($str){
$str = ltrim($str,"0") * 1;
$str = (string)$str;
$Big = array(0=>"",1=>"壹",2=>"贰",3=>"叁",4=>"肆",5=>"伍",6=>"陆",7=>"柒",8=>"捌",9=>"玖");
$loc = array(0=>"零",1=>"",2=>"拾",3=>"佰",4=>"仟");
$level = array("","万","亿","兆");
$result = NULL;
$len = ceil(strlen($str)/4);
$l = strlen($str)-1;
$arr = array();
//分段
for($i = 0;$i < $len;$i++){
for($p =0 ;$p < 4;$p++){
if($l == -1) break; $arr[$i] .= $str[$l--];
}
$arr[$i] = strrev($arr[$i]);
}
$cou = count($arr);
//分段转换
for($i = $cou -1;$i > -1;$i--){
//获取每段循环的次数
$lo = $O = strlen($arr[$i]);
//转换
for($m = 0;$m < $O;$m++){
//不是0
if($arr[$i][$m] != 0){
$locc = $loc[$lo];
}else{
//最后一位是0
if(($m+1) == $O){
$locc = NULL;
}else{//中间存在的0
if($arr[$i][$m + 1] != 0){//当下一个数字不为0的时候加“零”
$locc = $loc[0];
}else{
$locc = NULL;
}
}
}
$lo--;
$result .= $Big[$arr[$i][$m]].$locc;
}
$result .= $level[$i];
}
return $result;
}
echo trans($_GET["a"]);
@drunkday
Copy link
Author

小数点木有加,当然可以扩展,请指教

@hehexianshi
Copy link

你这个有点绕,直接 str_replace 里面传数组就好了啊!

@hehexianshi
Copy link

我看错了!失误

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment