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