-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
282 lines (263 loc) · 8.83 KB
/
functions.php
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
$numberToText_and_textToNumber_tokens = '/(\w+ion|thousand)/';
$numberToText_default_options = array(
'locale' => 'en',
'insert_ands' => 'many',
'insert_commas' => true,
'remove_hyphens' => false
);
/**
* Format a number "100" into text "one hundred"
*
* @param integer|double $number to format into text
* @param array $options to format number with
*
* @return string Formatted number as text
*/
function numberToText($number, $options = null) {
global $numberToText_default_options;
global $numberToText_and_textToNumber_tokens;
$options = array_merge(
$numberToText_default_options,
is_array($options) ? $options : $numberToText_default_options
);
/**
* Format the input number, using the given locale
*/
$NumberFormatter = new \NumberFormatter(
$options['locale'],
\NumberFormatter::SPELLOUT
);
$text = strtr(
$NumberFormatter->format($number),
array(
',' => ''
)
);
/**
* Insert one or many 'and' words into the number text
*/
switch ($options['insert_ands'] ?? $numberToText_default_options['insert_ands']) {
case 'many':
$text = strtr(
$text,
array(
'hundred ' => 'hundred and '
)
);
break;
case 'one':
if (strpos($text, ' hundred ') !== false) {
$text = explode(' hundred ', $text);
$text[count($text) - 1] = 'and ' . $text[count($text) - 1];
$text = implode(' hundred ', $text);
}
break;
}
/**
* Insert commas into the number text
*/
if ($options['insert_commas'] ?? $numberToText_default_options['insert_commas']) {
$text = preg_replace(
$numberToText_and_textToNumber_tokens,
'$1,',
$text
);
}
/**
* Remove hyphens from the produced output
*/
if ($options['remove_hyphens'] ?? $numberToText_default_options['remove_hyphens']) {
$text = strtr(
$text,
array(
'-' => ' '
)
);
}
/**
* Remove trailing comma, if needed
*/
if (substr($text, -1) == ',') {
$text = substr(
$text,
0,
strlen($text) - 1
);
}
return $text;
}
/**
* Format text "one hundred" into a number "100"
*
* @param string $text to format into number
*
* @return integer|double Formatted text as number
*/
function textToNumber($text) {
global $numberToText_and_textToNumber_tokens;
$lookup = array(
// word and corresponding number
'zero' => 0,
'one' => 1,
'two' => 2,
'three' => 3,
'four' => 4,
'five' => 5,
'six' => 6,
'seven' => 7,
'eight' => 8,
'nine' => 9,
'ten' => 10,
'eleven' => 11,
'twelve' => 12,
'thirteen' => 13,
'fourteen' => 14,
'fifteen' => 15,
'sixteen' => 16,
'seventeen' => 17,
'eighteen' => 18,
'nineteen' => 19,
'twenty' => 20,
'thirty' => 30,
'forty' => 40,
'fourty' => 40,
'fifty' => 50,
'sixty' => 60,
'seventy' => 70,
'eighty' => 80,
'ninety' => 90,
// word and corresponding power
'thousand' => 3,
'million' => 6,
'billion' => 9,
'trillion' => 12,
'quadrillion' => 15,
'quintillion' => 18,
'sextillion' => 21,
'septillion' => 24,
'octillion' => 27,
'nonillion' => 30,
'decillion' => 33,
'undecillion' => 36,
'duodecillion' => 39,
'tredecillion' => 42,
'quattuordecillion' => 45,
'quindecillion' => 48,
'sexdecillion' => 51,
'septendecillion' => 54,
'octodecillion' => 57,
'novemdecillion' => 60,
'vigintillion' => 63,
'centillion' => 303
);
$text = strtr(
strtolower($text),
array(
'hundred and ' => 'hundred ',
',' => ''
)
);
/**
* Split $text into an array of tokens, some examples,
* "one hundred and twenty three thousand three hundred and twenty one":
* [["thousand"]]
*
* "one hundred and twenty three million four hundred and fifty four thousand three hundred and twenty one":
* [["million", "thousand"]]
*/
preg_match_all($numberToText_and_textToNumber_tokens, $text, $word_tokens);
/**
* Split $text into an array without the tokens, some examples,
* "one hundred and twenty three thousand three hundred and twenty one":
* ["one hundred and twenty three", "three hundred and twenty one"]
*
* "one hundred and twenty three million four hundred and fifty four thousand three hundred and twenty one":
* ["one hundred and twenty three", "four hundred and fifty four", "three hundred and twenty one"]
*/
$word_segments = preg_split($numberToText_and_textToNumber_tokens, $text);
return array_reduce(
array_map(
function ($segment, $segment_i) use ($word_tokens, $lookup) {
/**
* Split the hundreds from the tens and the ones, some examples:
* "one hundred and twenty three":
* ["one", "twenty three"]
*
* "three hundred and twenty one":
* ["three", "twenty one"]
*
* "twenty two":
* ["twenty two"]
*/
$segment = explode(
'hundred',
trim($segment)
);
/**
* Ensure the array has a hundreds part, some examples,
* ["one", "twenty three"]:
* ["one", "twenty three"]
*
* ["twenty two"]:
* ["zero", "twenty two"]
*/
if (count($segment) == 1) {
array_unshift($segment, 'zero');
}
/**
* Multiply the calculated reduced number with power of the token
* Zero is used to ensure a power of 0 is used at the end of a word
*/
return array_reduce(
array_map(
function ($segment_part, $segment_part_i) use ($lookup) {
/**
* Calculate the sum of the hundreds and the tens/ones
* Then multiply them by 1 or 100, based on the segment part index
*/
return array_reduce(
/**
* Split remaining segment by a space to isolate specific words
* Then replace with lookup value to substitude word for number
*
* If no lookup is found, we keep the value - as it's likely a number,
* For example if the function input was "3 million", this would work
*/
array_map(
function ($segment_part_word) use ($lookup) {
return $lookup[$segment_part_word] ?? $segment_part_word;
},
preg_split(
'/ |-/',
trim($segment_part)
)
),
function ($total, $value) {
$value = $value ?: 0;
if (! is_numeric($value)) {
throw new \Exception(sprintf(
'Invalid Reduce, "%1$s"',
$value
));
}
return $total + $value;
}
) * ($segment_part_i ?: 100);
},
$segment,
array_keys($segment)
),
function ($total, $value) {
return $total + $value;
}
) * pow(10, $lookup[$word_tokens[0][$segment_i] ?? 'zero']);
},
$word_segments,
array_keys($word_segments)
),
function ($total, $value) {
return $total + $value;
}
);
}