1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Shared; |
4
|
|
|
|
5
|
|
|
class StringHelper |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Control characters array. |
9
|
|
|
* |
10
|
|
|
* @var string[] |
11
|
|
|
*/ |
12
|
|
|
private static $controlCharacters = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* SYLK Characters array. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private static $SYLKCharacters = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Decimal separator. |
23
|
|
|
* |
24
|
|
|
* @var ?string |
25
|
|
|
*/ |
26
|
|
|
private static $decimalSeparator; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Thousands separator. |
30
|
|
|
* |
31
|
|
|
* @var ?string |
32
|
|
|
*/ |
33
|
|
|
private static $thousandsSeparator; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Currency code. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private static $currencyCode; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Is iconv extension avalable? |
44
|
|
|
* |
45
|
|
|
* @var ?bool |
46
|
|
|
*/ |
47
|
|
|
private static $isIconvEnabled; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* iconv options. |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
private static $iconvOptions = '//IGNORE//TRANSLIT'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Build control characters array. |
58
|
|
|
*/ |
59
|
99 |
|
private static function buildControlCharacters(): void |
60
|
|
|
{ |
61
|
99 |
|
for ($i = 0; $i <= 31; ++$i) { |
62
|
99 |
|
if ($i != 9 && $i != 10 && $i != 13) { |
63
|
99 |
|
$find = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_'; |
64
|
99 |
|
$replace = chr($i); |
65
|
99 |
|
self::$controlCharacters[$find] = $replace; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Build SYLK characters array. |
72
|
|
|
*/ |
73
|
99 |
|
private static function buildSYLKCharacters(): void |
74
|
|
|
{ |
75
|
99 |
|
self::$SYLKCharacters = [ |
76
|
99 |
|
"\x1B 0" => chr(0), |
77
|
99 |
|
"\x1B 1" => chr(1), |
78
|
99 |
|
"\x1B 2" => chr(2), |
79
|
99 |
|
"\x1B 3" => chr(3), |
80
|
99 |
|
"\x1B 4" => chr(4), |
81
|
99 |
|
"\x1B 5" => chr(5), |
82
|
99 |
|
"\x1B 6" => chr(6), |
83
|
99 |
|
"\x1B 7" => chr(7), |
84
|
99 |
|
"\x1B 8" => chr(8), |
85
|
99 |
|
"\x1B 9" => chr(9), |
86
|
99 |
|
"\x1B :" => chr(10), |
87
|
99 |
|
"\x1B ;" => chr(11), |
88
|
99 |
|
"\x1B <" => chr(12), |
89
|
99 |
|
"\x1B =" => chr(13), |
90
|
99 |
|
"\x1B >" => chr(14), |
91
|
99 |
|
"\x1B ?" => chr(15), |
92
|
99 |
|
"\x1B!0" => chr(16), |
93
|
99 |
|
"\x1B!1" => chr(17), |
94
|
99 |
|
"\x1B!2" => chr(18), |
95
|
99 |
|
"\x1B!3" => chr(19), |
96
|
99 |
|
"\x1B!4" => chr(20), |
97
|
99 |
|
"\x1B!5" => chr(21), |
98
|
99 |
|
"\x1B!6" => chr(22), |
99
|
99 |
|
"\x1B!7" => chr(23), |
100
|
99 |
|
"\x1B!8" => chr(24), |
101
|
99 |
|
"\x1B!9" => chr(25), |
102
|
99 |
|
"\x1B!:" => chr(26), |
103
|
99 |
|
"\x1B!;" => chr(27), |
104
|
99 |
|
"\x1B!<" => chr(28), |
105
|
99 |
|
"\x1B!=" => chr(29), |
106
|
99 |
|
"\x1B!>" => chr(30), |
107
|
99 |
|
"\x1B!?" => chr(31), |
108
|
99 |
|
"\x1B'?" => chr(127), |
109
|
99 |
|
"\x1B(0" => '€', // 128 in CP1252 |
110
|
99 |
|
"\x1B(2" => '‚', // 130 in CP1252 |
111
|
99 |
|
"\x1B(3" => 'ƒ', // 131 in CP1252 |
112
|
99 |
|
"\x1B(4" => '„', // 132 in CP1252 |
113
|
99 |
|
"\x1B(5" => '…', // 133 in CP1252 |
114
|
99 |
|
"\x1B(6" => '†', // 134 in CP1252 |
115
|
99 |
|
"\x1B(7" => '‡', // 135 in CP1252 |
116
|
99 |
|
"\x1B(8" => 'ˆ', // 136 in CP1252 |
117
|
99 |
|
"\x1B(9" => '‰', // 137 in CP1252 |
118
|
99 |
|
"\x1B(:" => 'Š', // 138 in CP1252 |
119
|
99 |
|
"\x1B(;" => '‹', // 139 in CP1252 |
120
|
99 |
|
"\x1BNj" => 'Œ', // 140 in CP1252 |
121
|
99 |
|
"\x1B(>" => 'Ž', // 142 in CP1252 |
122
|
99 |
|
"\x1B)1" => '‘', // 145 in CP1252 |
123
|
99 |
|
"\x1B)2" => '’', // 146 in CP1252 |
124
|
99 |
|
"\x1B)3" => '“', // 147 in CP1252 |
125
|
99 |
|
"\x1B)4" => '”', // 148 in CP1252 |
126
|
99 |
|
"\x1B)5" => '•', // 149 in CP1252 |
127
|
99 |
|
"\x1B)6" => '–', // 150 in CP1252 |
128
|
99 |
|
"\x1B)7" => '—', // 151 in CP1252 |
129
|
99 |
|
"\x1B)8" => '˜', // 152 in CP1252 |
130
|
99 |
|
"\x1B)9" => '™', // 153 in CP1252 |
131
|
99 |
|
"\x1B):" => 'š', // 154 in CP1252 |
132
|
99 |
|
"\x1B);" => '›', // 155 in CP1252 |
133
|
99 |
|
"\x1BNz" => 'œ', // 156 in CP1252 |
134
|
99 |
|
"\x1B)>" => 'ž', // 158 in CP1252 |
135
|
99 |
|
"\x1B)?" => 'Ÿ', // 159 in CP1252 |
136
|
99 |
|
"\x1B*0" => ' ', // 160 in CP1252 |
137
|
99 |
|
"\x1BN!" => '¡', // 161 in CP1252 |
138
|
99 |
|
"\x1BN\"" => '¢', // 162 in CP1252 |
139
|
99 |
|
"\x1BN#" => '£', // 163 in CP1252 |
140
|
99 |
|
"\x1BN(" => '¤', // 164 in CP1252 |
141
|
99 |
|
"\x1BN%" => '¥', // 165 in CP1252 |
142
|
99 |
|
"\x1B*6" => '¦', // 166 in CP1252 |
143
|
99 |
|
"\x1BN'" => '§', // 167 in CP1252 |
144
|
99 |
|
"\x1BNH " => '¨', // 168 in CP1252 |
145
|
99 |
|
"\x1BNS" => '©', // 169 in CP1252 |
146
|
99 |
|
"\x1BNc" => 'ª', // 170 in CP1252 |
147
|
99 |
|
"\x1BN+" => '«', // 171 in CP1252 |
148
|
99 |
|
"\x1B*<" => '¬', // 172 in CP1252 |
149
|
99 |
|
"\x1B*=" => '', // 173 in CP1252 |
150
|
99 |
|
"\x1BNR" => '®', // 174 in CP1252 |
151
|
99 |
|
"\x1B*?" => '¯', // 175 in CP1252 |
152
|
99 |
|
"\x1BN0" => '°', // 176 in CP1252 |
153
|
99 |
|
"\x1BN1" => '±', // 177 in CP1252 |
154
|
99 |
|
"\x1BN2" => '²', // 178 in CP1252 |
155
|
99 |
|
"\x1BN3" => '³', // 179 in CP1252 |
156
|
99 |
|
"\x1BNB " => '´', // 180 in CP1252 |
157
|
99 |
|
"\x1BN5" => 'µ', // 181 in CP1252 |
158
|
99 |
|
"\x1BN6" => '¶', // 182 in CP1252 |
159
|
99 |
|
"\x1BN7" => '·', // 183 in CP1252 |
160
|
99 |
|
"\x1B+8" => '¸', // 184 in CP1252 |
161
|
99 |
|
"\x1BNQ" => '¹', // 185 in CP1252 |
162
|
99 |
|
"\x1BNk" => 'º', // 186 in CP1252 |
163
|
99 |
|
"\x1BN;" => '»', // 187 in CP1252 |
164
|
99 |
|
"\x1BN<" => '¼', // 188 in CP1252 |
165
|
99 |
|
"\x1BN=" => '½', // 189 in CP1252 |
166
|
99 |
|
"\x1BN>" => '¾', // 190 in CP1252 |
167
|
99 |
|
"\x1BN?" => '¿', // 191 in CP1252 |
168
|
99 |
|
"\x1BNAA" => 'À', // 192 in CP1252 |
169
|
99 |
|
"\x1BNBA" => 'Á', // 193 in CP1252 |
170
|
99 |
|
"\x1BNCA" => 'Â', // 194 in CP1252 |
171
|
99 |
|
"\x1BNDA" => 'Ã', // 195 in CP1252 |
172
|
99 |
|
"\x1BNHA" => 'Ä', // 196 in CP1252 |
173
|
99 |
|
"\x1BNJA" => 'Å', // 197 in CP1252 |
174
|
99 |
|
"\x1BNa" => 'Æ', // 198 in CP1252 |
175
|
99 |
|
"\x1BNKC" => 'Ç', // 199 in CP1252 |
176
|
99 |
|
"\x1BNAE" => 'È', // 200 in CP1252 |
177
|
99 |
|
"\x1BNBE" => 'É', // 201 in CP1252 |
178
|
99 |
|
"\x1BNCE" => 'Ê', // 202 in CP1252 |
179
|
99 |
|
"\x1BNHE" => 'Ë', // 203 in CP1252 |
180
|
99 |
|
"\x1BNAI" => 'Ì', // 204 in CP1252 |
181
|
99 |
|
"\x1BNBI" => 'Í', // 205 in CP1252 |
182
|
99 |
|
"\x1BNCI" => 'Î', // 206 in CP1252 |
183
|
99 |
|
"\x1BNHI" => 'Ï', // 207 in CP1252 |
184
|
99 |
|
"\x1BNb" => 'Ð', // 208 in CP1252 |
185
|
99 |
|
"\x1BNDN" => 'Ñ', // 209 in CP1252 |
186
|
99 |
|
"\x1BNAO" => 'Ò', // 210 in CP1252 |
187
|
99 |
|
"\x1BNBO" => 'Ó', // 211 in CP1252 |
188
|
99 |
|
"\x1BNCO" => 'Ô', // 212 in CP1252 |
189
|
99 |
|
"\x1BNDO" => 'Õ', // 213 in CP1252 |
190
|
99 |
|
"\x1BNHO" => 'Ö', // 214 in CP1252 |
191
|
99 |
|
"\x1B-7" => '×', // 215 in CP1252 |
192
|
99 |
|
"\x1BNi" => 'Ø', // 216 in CP1252 |
193
|
99 |
|
"\x1BNAU" => 'Ù', // 217 in CP1252 |
194
|
99 |
|
"\x1BNBU" => 'Ú', // 218 in CP1252 |
195
|
99 |
|
"\x1BNCU" => 'Û', // 219 in CP1252 |
196
|
99 |
|
"\x1BNHU" => 'Ü', // 220 in CP1252 |
197
|
99 |
|
"\x1B-=" => 'Ý', // 221 in CP1252 |
198
|
99 |
|
"\x1BNl" => 'Þ', // 222 in CP1252 |
199
|
99 |
|
"\x1BN{" => 'ß', // 223 in CP1252 |
200
|
99 |
|
"\x1BNAa" => 'à', // 224 in CP1252 |
201
|
99 |
|
"\x1BNBa" => 'á', // 225 in CP1252 |
202
|
99 |
|
"\x1BNCa" => 'â', // 226 in CP1252 |
203
|
99 |
|
"\x1BNDa" => 'ã', // 227 in CP1252 |
204
|
99 |
|
"\x1BNHa" => 'ä', // 228 in CP1252 |
205
|
99 |
|
"\x1BNJa" => 'å', // 229 in CP1252 |
206
|
99 |
|
"\x1BNq" => 'æ', // 230 in CP1252 |
207
|
99 |
|
"\x1BNKc" => 'ç', // 231 in CP1252 |
208
|
99 |
|
"\x1BNAe" => 'è', // 232 in CP1252 |
209
|
99 |
|
"\x1BNBe" => 'é', // 233 in CP1252 |
210
|
99 |
|
"\x1BNCe" => 'ê', // 234 in CP1252 |
211
|
99 |
|
"\x1BNHe" => 'ë', // 235 in CP1252 |
212
|
99 |
|
"\x1BNAi" => 'ì', // 236 in CP1252 |
213
|
99 |
|
"\x1BNBi" => 'í', // 237 in CP1252 |
214
|
99 |
|
"\x1BNCi" => 'î', // 238 in CP1252 |
215
|
99 |
|
"\x1BNHi" => 'ï', // 239 in CP1252 |
216
|
99 |
|
"\x1BNs" => 'ð', // 240 in CP1252 |
217
|
99 |
|
"\x1BNDn" => 'ñ', // 241 in CP1252 |
218
|
99 |
|
"\x1BNAo" => 'ò', // 242 in CP1252 |
219
|
99 |
|
"\x1BNBo" => 'ó', // 243 in CP1252 |
220
|
99 |
|
"\x1BNCo" => 'ô', // 244 in CP1252 |
221
|
99 |
|
"\x1BNDo" => 'õ', // 245 in CP1252 |
222
|
99 |
|
"\x1BNHo" => 'ö', // 246 in CP1252 |
223
|
99 |
|
"\x1B/7" => '÷', // 247 in CP1252 |
224
|
99 |
|
"\x1BNy" => 'ø', // 248 in CP1252 |
225
|
99 |
|
"\x1BNAu" => 'ù', // 249 in CP1252 |
226
|
99 |
|
"\x1BNBu" => 'ú', // 250 in CP1252 |
227
|
99 |
|
"\x1BNCu" => 'û', // 251 in CP1252 |
228
|
99 |
|
"\x1BNHu" => 'ü', // 252 in CP1252 |
229
|
99 |
|
"\x1B/=" => 'ý', // 253 in CP1252 |
230
|
99 |
|
"\x1BN|" => 'þ', // 254 in CP1252 |
231
|
99 |
|
"\x1BNHy" => 'ÿ', // 255 in CP1252 |
232
|
99 |
|
]; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Get whether iconv extension is available. |
237
|
|
|
* |
238
|
|
|
* @return bool |
239
|
|
|
*/ |
240
|
179 |
|
public static function getIsIconvEnabled() |
241
|
|
|
{ |
242
|
179 |
|
if (isset(self::$isIconvEnabled)) { |
243
|
179 |
|
return self::$isIconvEnabled; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
// Assume no problems with iconv |
247
|
72 |
|
self::$isIconvEnabled = true; |
248
|
|
|
|
249
|
|
|
// Fail if iconv doesn't exist |
250
|
72 |
|
if (!function_exists('iconv')) { |
251
|
|
|
self::$isIconvEnabled = false; |
252
|
72 |
|
} elseif (!@iconv('UTF-8', 'UTF-16LE', 'x')) { |
253
|
|
|
// Sometimes iconv is not working, and e.g. iconv('UTF-8', 'UTF-16LE', 'x') just returns false, |
254
|
|
|
self::$isIconvEnabled = false; |
255
|
72 |
|
} elseif (defined('PHP_OS') && @stristr(PHP_OS, 'AIX') && defined('ICONV_IMPL') && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && defined('ICONV_VERSION') && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) { |
256
|
|
|
// CUSTOM: IBM AIX iconv() does not work |
257
|
|
|
self::$isIconvEnabled = false; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
// Deactivate iconv default options if they fail (as seen on IMB i) |
261
|
72 |
|
if (self::$isIconvEnabled && !@iconv('UTF-8', 'UTF-16LE' . self::$iconvOptions, 'x')) { |
262
|
|
|
self::$iconvOptions = ''; |
263
|
|
|
} |
264
|
|
|
|
265
|
72 |
|
return self::$isIconvEnabled; |
266
|
|
|
} |
267
|
|
|
|
268
|
518 |
|
private static function buildCharacterSets(): void |
269
|
|
|
{ |
270
|
518 |
|
if (empty(self::$controlCharacters)) { |
271
|
99 |
|
self::buildControlCharacters(); |
272
|
|
|
} |
273
|
|
|
|
274
|
518 |
|
if (empty(self::$SYLKCharacters)) { |
275
|
99 |
|
self::buildSYLKCharacters(); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Convert from OpenXML escaped control character to PHP control character. |
281
|
|
|
* |
282
|
|
|
* Excel 2007 team: |
283
|
|
|
* ---------------- |
284
|
|
|
* That's correct, control characters are stored directly in the shared-strings table. |
285
|
|
|
* We do encode characters that cannot be represented in XML using the following escape sequence: |
286
|
|
|
* _xHHHH_ where H represents a hexadecimal character in the character's value... |
287
|
|
|
* So you could end up with something like _x0008_ in a string (either in a cell value (<v>) |
288
|
|
|
* element or in the shared string <t> element. |
289
|
|
|
* |
290
|
|
|
* @param string $textValue Value to unescape |
291
|
|
|
* |
292
|
|
|
* @return string |
293
|
|
|
*/ |
294
|
410 |
|
public static function controlCharacterOOXML2PHP($textValue) |
295
|
|
|
{ |
296
|
410 |
|
self::buildCharacterSets(); |
297
|
|
|
|
298
|
410 |
|
return str_replace(array_keys(self::$controlCharacters), array_values(self::$controlCharacters), $textValue); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Convert from PHP control character to OpenXML escaped control character. |
303
|
|
|
* |
304
|
|
|
* Excel 2007 team: |
305
|
|
|
* ---------------- |
306
|
|
|
* That's correct, control characters are stored directly in the shared-strings table. |
307
|
|
|
* We do encode characters that cannot be represented in XML using the following escape sequence: |
308
|
|
|
* _xHHHH_ where H represents a hexadecimal character in the character's value... |
309
|
|
|
* So you could end up with something like _x0008_ in a string (either in a cell value (<v>) |
310
|
|
|
* element or in the shared string <t> element. |
311
|
|
|
* |
312
|
|
|
* @param string $textValue Value to escape |
313
|
|
|
* |
314
|
|
|
* @return string |
315
|
|
|
*/ |
316
|
206 |
|
public static function controlCharacterPHP2OOXML($textValue) |
317
|
|
|
{ |
318
|
206 |
|
self::buildCharacterSets(); |
319
|
|
|
|
320
|
206 |
|
return str_replace(array_values(self::$controlCharacters), array_keys(self::$controlCharacters), $textValue); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Try to sanitize UTF8, replacing invalid sequences with Unicode substitution characters. |
325
|
|
|
*/ |
326
|
9397 |
|
public static function sanitizeUTF8(string $textValue): string |
327
|
|
|
{ |
328
|
9397 |
|
$textValue = str_replace(["\xef\xbf\xbe", "\xef\xbf\xbf"], "\xef\xbf\xbd", $textValue); |
329
|
9397 |
|
$subst = mb_substitute_character(); // default is question mark |
330
|
9397 |
|
mb_substitute_character(65533); // Unicode substitution character |
331
|
|
|
// Phpstan does not think this can return false. |
332
|
9397 |
|
$returnValue = mb_convert_encoding($textValue, 'UTF-8', 'UTF-8'); |
333
|
9397 |
|
mb_substitute_character(/** @scrutinizer ignore-type */ $subst); |
334
|
|
|
|
335
|
9397 |
|
return self::returnString($returnValue); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Strictly to satisfy Scrutinizer. |
340
|
|
|
* |
341
|
|
|
* @param mixed $value |
342
|
|
|
*/ |
343
|
9397 |
|
private static function returnString($value): string |
344
|
|
|
{ |
345
|
9397 |
|
return is_string($value) ? $value : ''; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Check if a string contains UTF8 data. |
350
|
|
|
*/ |
351
|
1 |
|
public static function isUTF8(string $textValue): bool |
352
|
|
|
{ |
353
|
1 |
|
return $textValue === self::sanitizeUTF8($textValue); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Formats a numeric value as a string for output in various output writers forcing |
358
|
|
|
* point as decimal separator in case locale is other than English. |
359
|
|
|
* |
360
|
|
|
* @param float|int|string $numericValue |
361
|
|
|
*/ |
362
|
743 |
|
public static function formatNumber($numericValue): string |
363
|
|
|
{ |
364
|
743 |
|
if (is_float($numericValue)) { |
365
|
742 |
|
return str_replace(',', '.', (string) $numericValue); |
366
|
|
|
} |
367
|
|
|
|
368
|
222 |
|
return (string) $numericValue; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Converts a UTF-8 string into BIFF8 Unicode string data (8-bit string length) |
373
|
|
|
* Writes the string using uncompressed notation, no rich text, no Asian phonetics |
374
|
|
|
* If mbstring extension is not available, ASCII is assumed, and compressed notation is used |
375
|
|
|
* although this will give wrong results for non-ASCII strings |
376
|
|
|
* see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3. |
377
|
|
|
* |
378
|
|
|
* @param string $textValue UTF-8 encoded string |
379
|
|
|
* @param mixed[] $arrcRuns Details of rich text runs in $value |
380
|
|
|
*/ |
381
|
91 |
|
public static function UTF8toBIFF8UnicodeShort(string $textValue, array $arrcRuns = []): string |
382
|
|
|
{ |
383
|
|
|
// character count |
384
|
91 |
|
$ln = self::countCharacters($textValue, 'UTF-8'); |
385
|
|
|
// option flags |
386
|
91 |
|
if (empty($arrcRuns)) { |
387
|
91 |
|
$data = pack('CC', $ln, 0x0001); |
388
|
|
|
// characters |
389
|
91 |
|
$data .= self::convertEncoding($textValue, 'UTF-16LE', 'UTF-8'); |
390
|
|
|
} else { |
391
|
11 |
|
$data = pack('vC', $ln, 0x09); |
392
|
11 |
|
$data .= pack('v', count($arrcRuns)); |
393
|
|
|
// characters |
394
|
11 |
|
$data .= self::convertEncoding($textValue, 'UTF-16LE', 'UTF-8'); |
395
|
11 |
|
foreach ($arrcRuns as $cRun) { |
396
|
11 |
|
$data .= pack('v', $cRun['strlen']); |
397
|
11 |
|
$data .= pack('v', $cRun['fontidx']); |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
|
401
|
91 |
|
return $data; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Converts a UTF-8 string into BIFF8 Unicode string data (16-bit string length) |
406
|
|
|
* Writes the string using uncompressed notation, no rich text, no Asian phonetics |
407
|
|
|
* If mbstring extension is not available, ASCII is assumed, and compressed notation is used |
408
|
|
|
* although this will give wrong results for non-ASCII strings |
409
|
|
|
* see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3. |
410
|
|
|
* |
411
|
|
|
* @param string $textValue UTF-8 encoded string |
412
|
|
|
*/ |
413
|
92 |
|
public static function UTF8toBIFF8UnicodeLong(string $textValue): string |
414
|
|
|
{ |
415
|
|
|
// character count |
416
|
92 |
|
$ln = self::countCharacters($textValue, 'UTF-8'); |
417
|
|
|
|
418
|
|
|
// characters |
419
|
92 |
|
$chars = self::convertEncoding($textValue, 'UTF-16LE', 'UTF-8'); |
420
|
|
|
|
421
|
92 |
|
return pack('vC', $ln, 0x0001) . $chars; |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* Convert string from one encoding to another. |
426
|
|
|
* |
427
|
|
|
* @param string $to Encoding to convert to, e.g. 'UTF-8' |
428
|
|
|
* @param string $from Encoding to convert from, e.g. 'UTF-16LE' |
429
|
|
|
*/ |
430
|
178 |
|
public static function convertEncoding(string $textValue, string $to, string $from): string |
431
|
|
|
{ |
432
|
178 |
|
if (self::getIsIconvEnabled()) { |
433
|
178 |
|
$result = iconv($from, $to . self::$iconvOptions, $textValue); |
434
|
178 |
|
if (false !== $result) { |
435
|
178 |
|
return $result; |
436
|
|
|
} |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
return self::returnString(mb_convert_encoding($textValue, $to, $from)); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Get character count. |
444
|
|
|
* |
445
|
|
|
* @param string $encoding Encoding |
446
|
|
|
* |
447
|
|
|
* @return int Character count |
448
|
|
|
*/ |
449
|
10190 |
|
public static function countCharacters(string $textValue, string $encoding = 'UTF-8'): int |
450
|
|
|
{ |
451
|
10190 |
|
return mb_strlen($textValue, $encoding); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Get character count using mb_strwidth rather than mb_strlen. |
456
|
|
|
* |
457
|
|
|
* @param string $encoding Encoding |
458
|
|
|
* |
459
|
|
|
* @return int Character count |
460
|
|
|
*/ |
461
|
63 |
|
public static function countCharactersDbcs(string $textValue, string $encoding = 'UTF-8'): int |
462
|
|
|
{ |
463
|
63 |
|
return mb_strwidth($textValue, $encoding); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* Get a substring of a UTF-8 encoded string. |
468
|
|
|
* |
469
|
|
|
* @param string $textValue UTF-8 encoded string |
470
|
|
|
* @param int $offset Start offset |
471
|
|
|
* @param ?int $length Maximum number of characters in substring |
472
|
|
|
*/ |
473
|
10162 |
|
public static function substring(string $textValue, int $offset, ?int $length = 0): string |
474
|
|
|
{ |
475
|
10162 |
|
return mb_substr($textValue, $offset, $length, 'UTF-8'); |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
/** |
479
|
|
|
* Convert a UTF-8 encoded string to upper case. |
480
|
|
|
* |
481
|
|
|
* @param string $textValue UTF-8 encoded string |
482
|
|
|
*/ |
483
|
9590 |
|
public static function strToUpper(string $textValue): string |
484
|
|
|
{ |
485
|
9590 |
|
return mb_convert_case($textValue, MB_CASE_UPPER, 'UTF-8'); |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* Convert a UTF-8 encoded string to lower case. |
490
|
|
|
* |
491
|
|
|
* @param string $textValue UTF-8 encoded string |
492
|
|
|
*/ |
493
|
9838 |
|
public static function strToLower(string $textValue): string |
494
|
|
|
{ |
495
|
9838 |
|
return mb_convert_case($textValue, MB_CASE_LOWER, 'UTF-8'); |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
/** |
499
|
|
|
* Convert a UTF-8 encoded string to title/proper case |
500
|
|
|
* (uppercase every first character in each word, lower case all other characters). |
501
|
|
|
* |
502
|
|
|
* @param string $textValue UTF-8 encoded string |
503
|
|
|
*/ |
504
|
19 |
|
public static function strToTitle(string $textValue): string |
505
|
|
|
{ |
506
|
19 |
|
return mb_convert_case($textValue, MB_CASE_TITLE, 'UTF-8'); |
507
|
|
|
} |
508
|
|
|
|
509
|
21 |
|
public static function mbIsUpper(string $character): bool |
510
|
|
|
{ |
511
|
21 |
|
return mb_strtolower($character, 'UTF-8') !== $character; |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
/** |
515
|
|
|
* Splits a UTF-8 string into an array of individual characters. |
516
|
|
|
*/ |
517
|
21 |
|
public static function mbStrSplit(string $string): array |
518
|
|
|
{ |
519
|
|
|
// Split at all position not after the start: ^ |
520
|
|
|
// and not before the end: $ |
521
|
21 |
|
$split = preg_split('/(?<!^)(?!$)/u', $string); |
522
|
|
|
|
523
|
21 |
|
return ($split === false) ? [] : $split; |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
/** |
527
|
|
|
* Reverse the case of a string, so that all uppercase characters become lowercase |
528
|
|
|
* and all lowercase characters become uppercase. |
529
|
|
|
* |
530
|
|
|
* @param string $textValue UTF-8 encoded string |
531
|
|
|
*/ |
532
|
21 |
|
public static function strCaseReverse(string $textValue): string |
533
|
|
|
{ |
534
|
21 |
|
$characters = self::mbStrSplit($textValue); |
535
|
21 |
|
foreach ($characters as &$character) { |
536
|
21 |
|
if (self::mbIsUpper($character)) { |
537
|
14 |
|
$character = mb_strtolower($character, 'UTF-8'); |
538
|
|
|
} else { |
539
|
17 |
|
$character = mb_strtoupper($character, 'UTF-8'); |
540
|
|
|
} |
541
|
|
|
} |
542
|
|
|
|
543
|
21 |
|
return implode('', $characters); |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
/** |
547
|
|
|
* Get the decimal separator. If it has not yet been set explicitly, try to obtain number |
548
|
|
|
* formatting information from locale. |
549
|
|
|
*/ |
550
|
1078 |
|
public static function getDecimalSeparator(): string |
551
|
|
|
{ |
552
|
1078 |
|
if (!isset(self::$decimalSeparator)) { |
553
|
33 |
|
$localeconv = localeconv(); |
554
|
33 |
|
self::$decimalSeparator = ($localeconv['decimal_point'] != '') |
555
|
33 |
|
? $localeconv['decimal_point'] : $localeconv['mon_decimal_point']; |
556
|
|
|
|
557
|
33 |
|
if (self::$decimalSeparator == '') { |
558
|
|
|
// Default to . |
559
|
|
|
self::$decimalSeparator = '.'; |
560
|
|
|
} |
561
|
|
|
} |
562
|
|
|
|
563
|
1078 |
|
return self::$decimalSeparator; |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
/** |
567
|
|
|
* Set the decimal separator. Only used by NumberFormat::toFormattedString() |
568
|
|
|
* to format output by \PhpOffice\PhpSpreadsheet\Writer\Html and \PhpOffice\PhpSpreadsheet\Writer\Pdf. |
569
|
|
|
* |
570
|
|
|
* @param string $separator Character for decimal separator |
571
|
|
|
*/ |
572
|
848 |
|
public static function setDecimalSeparator(string $separator): void |
573
|
|
|
{ |
574
|
848 |
|
self::$decimalSeparator = $separator; |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
/** |
578
|
|
|
* Get the thousands separator. If it has not yet been set explicitly, try to obtain number |
579
|
|
|
* formatting information from locale. |
580
|
|
|
*/ |
581
|
1078 |
|
public static function getThousandsSeparator(): string |
582
|
|
|
{ |
583
|
1078 |
|
if (!isset(self::$thousandsSeparator)) { |
584
|
33 |
|
$localeconv = localeconv(); |
585
|
33 |
|
self::$thousandsSeparator = ($localeconv['thousands_sep'] != '') |
586
|
33 |
|
? $localeconv['thousands_sep'] : $localeconv['mon_thousands_sep']; |
587
|
|
|
|
588
|
33 |
|
if (self::$thousandsSeparator == '') { |
589
|
|
|
// Default to . |
590
|
|
|
self::$thousandsSeparator = ','; |
591
|
|
|
} |
592
|
|
|
} |
593
|
|
|
|
594
|
1078 |
|
return self::$thousandsSeparator; |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
/** |
598
|
|
|
* Set the thousands separator. Only used by NumberFormat::toFormattedString() |
599
|
|
|
* to format output by \PhpOffice\PhpSpreadsheet\Writer\Html and \PhpOffice\PhpSpreadsheet\Writer\Pdf. |
600
|
|
|
* |
601
|
|
|
* @param string $separator Character for thousands separator |
602
|
|
|
*/ |
603
|
848 |
|
public static function setThousandsSeparator(string $separator): void |
604
|
|
|
{ |
605
|
848 |
|
self::$thousandsSeparator = $separator; |
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
/** |
609
|
|
|
* Get the currency code. If it has not yet been set explicitly, try to obtain the |
610
|
|
|
* symbol information from locale. |
611
|
|
|
*/ |
612
|
875 |
|
public static function getCurrencyCode(): string |
613
|
|
|
{ |
614
|
875 |
|
if (!empty(self::$currencyCode)) { |
615
|
875 |
|
return self::$currencyCode; |
616
|
|
|
} |
617
|
4 |
|
self::$currencyCode = '$'; |
618
|
4 |
|
$localeconv = localeconv(); |
619
|
4 |
|
if (!empty($localeconv['currency_symbol'])) { |
620
|
4 |
|
self::$currencyCode = $localeconv['currency_symbol']; |
621
|
|
|
|
622
|
4 |
|
return self::$currencyCode; |
623
|
|
|
} |
624
|
|
|
if (!empty($localeconv['int_curr_symbol'])) { |
625
|
|
|
self::$currencyCode = $localeconv['int_curr_symbol']; |
626
|
|
|
|
627
|
|
|
return self::$currencyCode; |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
return self::$currencyCode; |
631
|
|
|
} |
632
|
|
|
|
633
|
|
|
/** |
634
|
|
|
* Set the currency code. Only used by NumberFormat::toFormattedString() |
635
|
|
|
* to format output by \PhpOffice\PhpSpreadsheet\Writer\Html and \PhpOffice\PhpSpreadsheet\Writer\Pdf. |
636
|
|
|
* |
637
|
|
|
* @param string $currencyCode Character for currency code |
638
|
|
|
*/ |
639
|
848 |
|
public static function setCurrencyCode(string $currencyCode): void |
640
|
|
|
{ |
641
|
848 |
|
self::$currencyCode = $currencyCode; |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
/** |
645
|
|
|
* Convert SYLK encoded string to UTF-8. |
646
|
|
|
* |
647
|
|
|
* @param string $textValue SYLK encoded string |
648
|
|
|
* |
649
|
|
|
* @return string UTF-8 encoded string |
650
|
|
|
*/ |
651
|
8 |
|
public static function SYLKtoUTF8(string $textValue): string |
652
|
|
|
{ |
653
|
8 |
|
self::buildCharacterSets(); |
654
|
|
|
|
655
|
|
|
// If there is no escape character in the string there is nothing to do |
656
|
8 |
|
if (strpos($textValue, '') === false) { |
657
|
7 |
|
return $textValue; |
658
|
|
|
} |
659
|
|
|
|
660
|
2 |
|
foreach (self::$SYLKCharacters as $k => $v) { |
661
|
2 |
|
$textValue = str_replace($k, $v, $textValue); |
662
|
|
|
} |
663
|
|
|
|
664
|
2 |
|
return $textValue; |
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
/** |
668
|
|
|
* Retrieve any leading numeric part of a string, or return the full string if no leading numeric |
669
|
|
|
* (handles basic integer or float, but not exponent or non decimal). |
670
|
|
|
* |
671
|
|
|
* @param string $textValue |
672
|
|
|
* |
673
|
|
|
* @return mixed string or only the leading numeric part of the string |
674
|
|
|
*/ |
675
|
143 |
|
public static function testStringAsNumeric($textValue) |
676
|
|
|
{ |
677
|
143 |
|
if (is_numeric($textValue)) { |
678
|
142 |
|
return $textValue; |
679
|
|
|
} |
680
|
3 |
|
$v = (float) $textValue; |
681
|
|
|
|
682
|
3 |
|
return (is_numeric(substr($textValue, 0, strlen((string) $v)))) ? $v : $textValue; |
683
|
|
|
} |
684
|
|
|
} |
685
|
|
|
|