将数字转化成对应的汉字
1 /** 2 * 数字转化成对应的汉字 mun2ch( 1 ); 3 * @param unknown $num 4 * @param string $mode 5 * @return string 6 */ 7 8 public function num2ch($num) //整体读取转换 9 { 10 $num_real = $this->del0($num);//去掉前面的“0” 11 $numlen = strlen($num_real); 12 13 if($numlen >= 9)//如果满九位,读取“亿”位 14 { 15 $y=substr($num_real, -9, 1); 16 //echo $y; 17 $wsbq = substr($num_real, -8, 4); 18 $gsbq = substr($num_real, -4); 19 $a = $this->num_r($this->del0($gsbq)); 20 $b = $this->num_r($this->del0($wsbq))."万"; 21 $c = $this->num_r($this->del0($y))."亿"; 22 } 23 elseif($numlen <= 8 && $numlen >= 5) //如果大于等于“万” 24 { 25 $wsbq = substr($num_real, 0, $numlen-4); 26 $gsbq = substr($num_real, -4); 27 $a = $this->num_r($this->del0($gsbq)); 28 $b = $this->num_r($this->del0($wsbq))."万"; 29 $c=""; 30 } 31 elseif($numlen <= 4) //如果小于等于“千” 32 { 33 $gsbq = substr( $num_real, -$numlen); 34 $a = $this->num_r($this->del0($gsbq)); 35 $b=""; 36 $c=""; 37 } 38 $ch_num = $c.$b.$a; 39 40 return $ch_num ; 41 } 42 43 public function del0($num) //去掉数字段前面的0 44 { 45 return "".intval($num); 46 } 47 48 public function n2c($x) //单个数字变汉字 49 { 50 $arr_n = array("零","一","二","三","四","五","六","七","八","九","十"); 51 return $arr_n[$x]; 52 } 53 54 55 public function num_r($abcd) //读取数值(4位) 56 { 57 $arr= array(); 58 $str = ""; //读取后的汉字数值 59 $flag = 0; //该位是否为零 60 $flag_end = 1; //是否以“零”结尾 61 $size_r = strlen($abcd); 62 for($i=0; $i<$size_r; $i++) 63 { 64 $arr[$i] = $abcd{$i}; 65 } 66 $arrlen = count($arr); 67 for($j=0; $j<$arrlen; $j++) 68 { 69 $ch = $this->n2c($arr[$arrlen-1-$j]); //从后向前转汉字 70 71 if($ch == "零" && $flag == 0) 72 { //如果是第一个零 73 $flag = 1; //该位为零 74 $str = $ch.$str; //加入汉字数值字符串 75 continue; 76 } 77 elseif($ch == "零") 78 { //如果不是第一个零了 79 continue; 80 } 81 $flag = 0; //该位不是零 82 switch($j) 83 { 84 case 0: $str = $ch; $flag_end = 0; break; //第一位(末尾),没有以“零”结尾 85 case 1: $str = $ch."十".$str; break; //第二位 86 case 2: $str = $ch."百".$str; break; //第三位 87 case 3: $str = $ch."千".$str; break; //第四位 88 } 89 } 90 if($flag_end == 1) //如果以“零”结尾 91 { 92 mb_internal_encoding("UTF-8"); 93 $str = mb_substr($str, 0, mb_strlen($str)-1); //把“零”去掉 94 } 95 return $str; 96 }