A-A+

ecshop用中文显示商品的价格

2015年07月07日 PHP开源系统 暂无评论 阅读 11 views 次

经常玩 ecshop 或常常购物的小伙伴,知道购物商城中的价格是以数字来显示的,今天在群里遇到一个小伙伴问这个数字能不能改成中文来显示呢,额,这样的问题还是第一次遇到,这个倒也不是很难,只需要定义一个中文显示价格的函数再进行调用即可,下面是夏日博客从网上搜集过来的一个函数方法,我们来看一下。

第一步:首先在 /includes/lib_common.php 尾部加入下面函数。

 function number2Chinese($num, $m = 1)
{ switch($m)
{  case 0:
   $CNum = array(
             array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖'),array('','拾','佰','仟'),
                array('','萬','億','萬億')
          );
         break;
              default:
                     $CNum = array(
                                          array('零','一','二','三','四','五','六','七','八','九'),
                                          array('','十','百','千'),
                                          array('','万','亿','万亿')
                                          );
   break; }
       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;
}

第二步:对需要转换的变量,引用上面的函数“number2Chinese”,也就是显示商品价格的地方,例如,我想对商品详情页中的商品总价更改,定位到其代码中的变量为$price ( /includes/lib_common.php文件第97:0行),如下所示:

}
else
{
   $price = number_format(floatval($price),2,',','');
}
return sprintf($GLOBALS['_CFG']['currency_format'],$price);

在 reurn 一行上方添加:$price = number2Chinese($price);如下所示:

}
else
{
   $price = number_format(floatval($price),2,',','');
}
$price = number2Chinese($price);
return sprintf($GLOBALS['_CFG']['currency_format'],$price);

OK,所有的操作都已经做完了,现在去前台看一下最终结果吧,如果还是显示数字价格的话,就在后台清理一下缓存重新生成一下。

标签:

给我留言