PHP人民币大小写转换类(MoneyConvertor_CLS)

人民币大小写转换类 PHP:

  1. /**  
  2.  * MoneyConvertor Library For PHP 
  3.  * 人民币大小写转换类 
  4.  * --------------------------------------------------- 
  5.  * @site  https://www.fity.cn/post/534/ 
  6.  * @describe   对人民币进行大小写转换的类,该方法可以完美转换任何形式小写货币格式为人民币大写 
  7.  */  
  8.   
  9. final class MoneyConvertor {  
  10.     
  11.   //大写数字  
  12.   private $NUMBER_STR = array(  
  13.     "零","壹","贰","叁","肆","伍","陆","柒","捌","玖"  
  14.   );  
  15.     
  16.   //整数位货币单位  
  17.   private $I_UNIT_STR = array(  
  18.     "元","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟"  
  19.   );  
  20.     
  21.   //小数位货币单位  
  22.   private $D_UNIT_STR = array(  
  23.     "角","分","厘"  
  24.   );  
  25.     
  26.   //转换结果  
  27.   private $resultString = null;  
  28.   
  29.   /** 
  30.    * 使用一个小写数字金额的字符串来转换resultString对象 
  31.    * --------------------------------------------------- 
  32.    * @param  $numberStr    将要转换的小写数字金额 
  33.    * @return   $this->resultString 
  34.    */  
  35.   
  36.   public function convert($numberStr) {  
  37.       
  38.     //处理小数位为0  
  39.     if(preg_match('/^[0-9]+\.[0]+$/',$numberStr))  
  40.       $numberStr = intval($numberStr);  
  41.       
  42.     //补齐类似.5这样的无整数位数字  
  43.     if(substr($numberStr, 0, 1) == '.')  
  44.       number_format($numberStr);  
  45.       
  46.     //如果带逗号分隔符的数字  
  47.     if(strpos($numberStr','))  
  48.       $numberStr = str_replace(",","",$numberStr);  
  49.       
  50.     //判断是否为数字  
  51.     if (!is_numeric($numberStr))  
  52.       return '不是有效的货币数值';  
  53.       
  54.     //执行转换  
  55.     self::convertor($numberStr);  
  56.       
  57.     //返回转换结果  
  58.     return $this->resultString;  
  59.   }  
  60.     
  61.     
  62.   /** 
  63.    * 执行转换 
  64.    * --------------------------------------------------- 
  65.    * @param  $numberStr    将要转换的小写数字金额 
  66.    * @return   void 
  67.    */  
  68.   private function convertor($numberStr){  
  69.     //分差整数与浮点位,整数和小数部分分开,分别进行转换  
  70.     $cutedNumber = explode('.', (string)$numberStr);  
  71.   
  72.     //如果只有整数部分  
  73.     if (count($cutedNumber) == 1) {  
  74.       self::convertInteger($numberStr, TRUE);  
  75.     } else {  
  76.       self::convertInteger($cutedNumber[0]);  
  77.       self::convertDecimal($cutedNumber[1]);  
  78.     }  
  79.   
  80.     //去除无用零字符  
  81.     self::removeZero();  
  82.   }  
  83.     
  84.   /** 
  85.    * 对整数部分进行转换 
  86.    * ------------------------------------------------------------------ 
  87.    * @param  $integer        将要转换的小写数字整数部分 
  88.    * @param  $without_fractional    是否原数不带浮点数,即在最后显示“整” 
  89.    * 
  90.    * @return   $this 
  91.    */  
  92.   private function convertInteger($integer$without_fractional = false) {  
  93.     $resultString = null;  
  94.         
  95.     for ($i = 0; $i < strlen($integer); $i++) {  
  96.       $resultString .= $this->I_UNIT_STR[$i];  
  97.       $resultString .= $this->NUMBER_STR[substr(strrev($integer), $i, 1)];  
  98.     }  
  99.     //如果没有小数位  
  100.     $tidy = $without_fractional == false ? '' : '整';  
  101.     $this->resultString = self::str_reverse($resultString) . $tidy;  
  102.   
  103.     return $this;  
  104.   }  
  105.   
  106.   /** 
  107.    * 对小数点后三位部分进行转换 
  108.    * ------------------------------------------------------------------ 
  109.    * @param  $integer        将要转换的小数点后三位部分 
  110.    * @return   $this 
  111.    */  
  112.   private function convertDecimal($decimal) {  
  113.       
  114.     $resultString = null;  
  115.       
  116.     for ($i = 0; $i < strlen($decimal); $i++) {  
  117.       $resultString .= $this->NUMBER_STR[substr($decimal$i, 1)];  
  118.       $resultString .= $this->D_UNIT_STR[$i];  
  119.     }  
  120.     $this->resultString .= $resultString;  
  121.   
  122.     return $this;  
  123.   }  
  124.   
  125.   /** 
  126.    * 去掉多余的"零X" 
  127.    * ------------------------------------------------------------------ 
  128.    * @return   $this 
  129.    */  
  130.   private function removeZero() {  
  131.     while (strpos($this->resultString, "零拾") || strpos($this->resultString, "零佰") || strpos($this->resultString, "零仟") || strpos($this->resultString, "零万") || strpos($this->resultString, "零亿") || strpos($this->resultString, "零角") || strpos($this->resultString, "零分") || strpos($this->resultString, "零厘") || strpos($this->resultString, "零零") || strpos($this->resultString, "亿万") || strpos($this->resultString, "零元")) {  
  132.       $this->resultString = str_replace("零拾""零"$this->resultString);  
  133.       $this->resultString = str_replace("零佰""零"$this->resultString);  
  134.       $this->resultString = str_replace("零仟""零"$this->resultString);  
  135.       $this->resultString = str_replace("零万""万"$this->resultString);  
  136.       $this->resultString = str_replace("零亿""亿"$this->resultString);  
  137.       $this->resultString = str_replace("零角""零"$this->resultString);  
  138.       $this->resultString = str_replace("零分""零"$this->resultString);  
  139.       $this->resultString = str_replace("零厘""零"$this->resultString);  
  140.       $this->resultString = str_replace("零零""零"$this->resultString);  
  141.       $this->resultString = str_replace("亿万""亿"$this->resultString);  
  142.       $this->resultString = str_replace("零元""元"$this->resultString);  
  143.     }  
  144.   
  145.     return $this;  
  146.   }  
  147.   
  148.   /** 
  149.    * 中文UTF-8字符串反转 
  150.    * ------------------------------------------------------------------ 
  151.    * @param   $str  需要转换的UTF-8字符串 
  152.    * @return   void 
  153.    */  
  154.   function str_reverse($str) {  
  155.     //判断输入的是不是utf8类型的字符,否则退出  
  156.     if (!is_string($str) || !mb_check_encoding($str'UTF-8')) {  
  157.       return;  
  158.     }  
  159.     $array = array();  
  160.     //将字符串存入数组  
  161.     $l = mb_strlen($str'UTF-8');  
  162.     for ($i = 0; $i < $l$i++) {  
  163.       $array[] = mb_substr($str$i, 1, 'UTF-8');  
  164.     }  
  165.     //反转字符串  
  166.     krsort($array);  
  167.     //拼接字符串  
  168.     $string = implode($array);  
  169.     return $string;  
  170.   }  
  171.   
  172. }  


使用方法:

  1. require_once  'MoneyConvertor.php';  
  2. $mc = new MoneyConvertor();  
  3. //数字类型  
  4. echo $mc->convert(1000.00);  
  5. //字符串类型  
  6. echo $mc->convert('1.322');  
  7. //特殊字符串逗号分割类型  
  8. echo $mc->convert('100,000,000.00');  
  9. //特殊字符串无整数位类型  
  10. echo $mc->convert('.5');  



本文最后更新于 2015-08-11 14:30:58 并被添加「php php类库」标签,已有 3973 位童鞋阅读过。
本文作者:未来往事
本站使用「署名 4.0 国际」创作共享协议,可自由转载、引用,但需署名作者且注明文章出处

相关文章

此处评论已关闭