php如何實現(xiàn)數(shù)字轉(zhuǎn)漢字

發(fā)布時間:2024-01-27
php數(shù)字轉(zhuǎn)漢字的實現(xiàn)方法:首先創(chuàng)建一個php代碼示例文件;然后定義一個“number2chinese”方法;接著在方法體中通過switch循環(huán)語句實現(xiàn)轉(zhuǎn)換邏輯;最后執(zhí)行該文件即可。
推薦:《php教程》
php- 數(shù)字轉(zhuǎn)漢字
//數(shù)字轉(zhuǎn)漢字function number2chinese($num, $m = 1){ switch($m) { case 0: $cnum = array( array('零','壹','貳','叁','肆','伍','陸','柒','捌','玖'), array('','拾','佰','仟'), array('','萬','億','萬億') ); break; default: $cnum = array( array('零','一','二','三','四','五','六','七','八','九'), array('','十','百','千'), array('','萬','億','萬億') ); break; } // $cnum = array('零','一','二','三','四','五','六','七','八','九'); if (is_integer($num)) { $int = (string)$num; } else if (is_numeric($num)) { $num = explode('.', (string)floatval($num)); $int = $num[0]; $fl = isset($num[1]) ? $num[1] : false; } // 長度 $len = strlen($int); // 中文 $chinese = array(); // 反轉(zhuǎn)的數(shù)字 $str = strrev($int); for($i = 0; $i<$len; $i =4 ) { $s = array(0=>$str[$i], 1=>$str[$i 1], 2=>$str[$i 2], 3=>$str[$i 3]); $j = ''; // 千位 if ($s[3] !== '') { $s[3] = (int) $s[3]; if ($s[3] !== 0) { $j .= $cnum[0][$s[3]].$cnum[1][3]; } else { if ($s[2] != 0 || $s[1] != 0 || $s[0]!=0) { $j .= $cnum[0][0]; } } } // 百位 if ($s[2] !== '') { $s[2] = (int) $s[2]; if ($s[2] !== 0) { $j .= $cnum[0][$s[2]].$cnum[1][2]; } else { if ($s[3]!=0 && ($s[1] != 0 || $s[0]!=0) ) { $j .= $cnum[0][0]; } } } // 十位 if ($s[1] !== '') { $s[1] = (int) $s[1]; if ($s[1] !== 0) { $j .= $cnum[0][$s[1]].$cnum[1][1]; } else { if ($s[0]!=0 && $s[2] != 0) { $j .= $cnum[0][$s[1]]; } } } // 個位 if ($s[0] !== '') { $s[0] = (int) $s[0]; if ($s[0] !== 0) { $j .= $cnum[0][$s[0]].$cnum[1][0]; } else { // $j .= $cnum[0][0]; } } $j.=$cnum[2][$i/4]; array_unshift($chinese, $j); } $chs = implode('', $chinese); if ($fl) { $chs .= '點'; for($i=0,$j=strlen($fl); $i<$j; $i ) { $t = (int)$fl[$i]; $chs.= $str[0][$t]; } } return $chs;}
上一個:橡皮樹的栽培技術(shù)
下一個:“二梅”迎春光 中華新世界

羅湖附近哪里有學電腦培訓班,深圳市羅湖區(qū)哪個電腦培訓中心較好
德國thermokon差壓開關(guān)
Meriam流量計Meriam通訊器
2023年新手機(2022年準備上市的手機)
高考準考證遷安景區(qū)優(yōu)惠信息匯總
直流高壓發(fā)生器的常見故障及處理
RTT059531BTP現(xiàn)貨庫存,最新價格
9款健身益壽的代茶飲功效好
一文帶你了解:普洱茶拼配史
小愛同學3.0怎么升級鴻蒙系統(tǒng)(小愛同學3.0怎么升級新版本)
十八禁 网站在线观看免费视频_2020av天堂网_一 级 黄 色 片免费网站_绝顶高潮合集Videos