php 阿拉伯数字转为中文汉字大、小写的简单示例

PHP阿拉伯数字转化为中文汉字(大、小写)感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!

/**
 * PHP阿拉伯数字转化为中文汉字(大、小写)
 *
 * @param 
 * @arrange 网: www.jb51.cc
 **/
function number2Chinese($num,$m = 1) {
 switch($m) {
  case 0:
   $CNum = array(
	  array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖'),array('','拾','佰','仟'),'萬','億','萬億')
	 );
   break;
  default:
   $CNum = 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();
 // 反转的数字
 $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;
}



/***   来自编程之家 jb51.cc(jb51.cc)   ***/

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...