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