1. 程式人生 > >php 數字轉換為中文

php 數字轉換為中文

中文 urn 目標 bstr ger 數字 pre array uml

/**
* 數字轉換為中文
* @param integer $num 目標數字
*/
public function number2chinese($num)
{
if (is_int($num) && $num < 100) {
$char = array(‘零‘, ‘一‘, ‘二‘, ‘三‘, ‘四‘, ‘五‘, ‘六‘, ‘七‘, ‘八‘, ‘九‘);
$unit = [‘‘, ‘十‘, ‘百‘, ‘千‘, ‘萬‘];
$return = ‘‘;
if ($num < 10) {
$return = $char[$num];
} elseif ($num%10 == 0) {
$firstNum = substr($num, 0, 1);
if ($num != 10) $return .= $char[$firstNum];
$return .= $unit[strlen($num) - 1];
} elseif ($num < 20) {
$return = $unit[substr($num, 0, -1)]. $char[substr($num, -1)];
} else {
$numData = str_split($num);
$numLength = count($numData) - 1;
foreach ($numData as $k => $v) {
if ($k == $numLength) continue;
$return .= $char[$v];
if ($v != 0) $return .= $unit[$numLength - $k];
}
$return .= $char[substr($num, -1)];
}
return $return;
}
}

php 數字轉換為中文